This is a problem with the java algorithm.

Asked 2 years ago, Updated 2 years ago, 47 views

https://www.hackerrank.com/challenges/30-linked-list/problem

This is the question above. First, you get the number of nodes, then you get the data value inside the node I think it's a matter of final outputting the input data value.

Class Node {
    int data;
    Node next;
    Node(int d) {
        data = d;
        next = null;
    }
}

class Solution { public static  Node insert(Node head,int data) {

        Node node = new Node(data);

        if (head == null)
            return node;

        Node start = head;
        while (start.next != null) {
            start = start.next;
        }

        start.next = node;

        return head;
    }
    public static void display(Node head) {
        Node start = head;
        while(start != null) {
            System.out.print(start.data + " ");
            start = start.next;
        }
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Node head = null;
        int N = sc.nextInt();

        while(N-- > 0) {
            int ele = sc.nextInt();
            head = insert(head,ele);
        }
        display(head);
        sc.close();
    }
}

The above is the final answer and the part that needs to be filled in the Solution class. After data enters the Node class and the first node you enter is declared head, If there is no head, return node? I don't know what I'm talking about. When you solve it with Python, it's two lines, so it's very confusing if you solve it with java. I don't know where you created as many nodes as you entered.ㅠㅠ n or maybe there's a next so you don't need to input nodes... Why doesn't that function show the first input value and then only the next value, data, is output. I'm curious.

algorithm java

2022-09-22 12:49

2 Answers

Hi, everyone. I don't know much about java, but there is no answer, so I write it down. I hope I don't confuse you any more.

1 - What is a Linked List? First of all, these data structures are called Singly Linked Lists, which are data structures that store data in a single line. And nodes that often appear in the data structure are spaces that store each data, which is convenient to think of as a connection point that connects the two lines.

2 - Then why do you call it a link list? That means that each node has a pointer, or address, to another node.

(Source: Wiki)

If you look at the image here, each black box is a node, the left purple represents the data, the value you want to save, and the right blue represents the pointer to the next node. Java probably doesn't have a pointer and it's all reference, but the basic structure is similar to what you've already created.

Additionally, the head here is the node at the very front. If you ask me why I need this, please look at the picture again. Let's say the first node is the current node. And let's say the second node is the next node, and the third node is the next.

To find the second node (next node node.next), you must find the previous node (current node node). To find the third node? Find the current node (node) and then find the node (node.next) to find the (node.next.next) in it. Head is the first level of the game to find all the other nodes (high level of the game) if you type in the list of connections here, you can understand that it's a level 1 area.

3 - So how are nodes created?

I don't know where you created as many nodes as you input

The difference between a linked list and an Array is that an array essentially needs an absolute size and cannot be resized. I understand that the same is true in Java. However, because each node already has the next node in the connection list, you can constantly create new nodes by resetting the value of the next node with nothing null. Simply put, it's an infinite array.

// Define numbers in edge array
arry_data[0] = 5;
// Add value to linked list
linkedlist.insert(5);
// Form a new node
Node node = new Node(data);
...
// Connect to head or head next node in this way
// The correct storage points will be described in section 4.
head = node; 
head.next = node;

4 - Practical code description Barabam Barabam...

Create a new node. It's not necessary, but I use it at least twice, so it's easier to just make it.

Node new_node=newNode(data); 

If the head is empty, it means the list is empty. then You don't have to find the next node, you can add data to that head node (by connecting the newly created node).

if (head == null)
{
    head = new_node;
}

If you have to find the last node because there is more than one node in this connection list, let's make a temporary head. This is what happens if you're going to put the next data together at the end. The reason for making this is the loop method of finding the last node. I'll explain it in a minute.

else
{
    Node temp_head = head;

How long do I have to find it using the while statement? Until the next node (next) of the current node is empty. If you look down here, you can just turn the head node into the next node to find the next node. So, because we couldn't find the head, we created a temporary head node. If you continue to change the current node (temporary head node), the value (of course) is null, i.e. a node that has not yet been created.

    while(temp_head.next != null)
    {
         temp_head = temp_head.next;  
    }

Next time, just connect it like if (head == null).

    temp_head.next = new_node;
}

I put the output at the end because I like to use only one return sentence as much as possible. And

return node; // not
Return head; // This is correct.

I'm explaining by writing node, but the question also tells me to return the head as much as possible.

your insert function should return a reference to the head node of the linked list

The reason why I get the first value even if I return node here is because the list and head are empty now. The head is also a node, and the main adds the return value to the head. As long as the result has only one value, it doesn't matter if you just make a node with the data you received and return it. But then you have multiple return locations and eventually you don't actually make a list in the insert function, you don't recommend it's not recommended. Of course, else... You can't use the newly created node.

5 - Explanation of additional questions

Why does that function not show the first input value and only the next value, data...

The person above also answered in detail, but it comes out in the question.

The first line contains T, the number of test cases.

The first value entered is the number of values to be tested. That is, if 4, 2, 3, 4, 1 then 4 only tells you how many times to test with 2, 3, 4, 1.


2022-09-22 12:49

public static void main (String args[]) {
        Scanner sc = new Scanner(System.in);
        Node head = null;
        int N = sc.nextInt(); // Store the first integer entered in N
    // N stands for the number of nodes to add (meaning the number of iterations of the while statement)

        while(N-- > 0) {
            intel = sc.nextInt(); // Subsequent numbers entered are numbers to be stored on the node
            head = insert(head,ele);
        }
        display(head);
        sc.close();
    }

Here, sc.nextInt() is the same as int(input()) of Python.


2022-09-22 12:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.