Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

java - Why head node is not changing in printLL() function?

I am trying to print a linked list. Both head and node are references of each other but still, the head is not changing after while loop in printLL() function. After end of while loop node is null but head is still pointing to 10. Why this is happening?

class Node { 
    int data;
    Node next;

    Node(int value) {
        this.data = value;
        this.next = null;
    }
}

class LL {
    Node head;

    void createLL() {
        head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(30);
        head.next.next.next = new Node(40);
    }

    void printLL() {
        Node node = head;
        System.out.println("Printing full Linked List");

        while (node != null) {
            System.out.println(node.data);
            node = node.next;
        }
        System.out.println("Value of head is " + head.data);
        if (head.equals(node)) {
            System.out.println("head and node both are same");
        }

        else {
            System.out.println("head and node are not same");`
        }

    }
}



// Output - 

// Printing full Linked List
// 10
// 20
// 30
// 40
// Value of head is 10
// head and node are not same
question from:https://stackoverflow.com/questions/65921128/why-head-node-is-not-changing-in-printll-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What is a Linked List ?

A linked list is formed by nodes that are linked together like a chain. Each node holds data, along with a pointer to the next node in the list.

The following image shows the basic structure of a Singly Linked List.

As you can see the Singly Linked List contains a head node: a pointer pointing to the first element of the list. Whenever we want to traverse the list, we can do so by using this head node.

Now using the below method of createLL(), you have created a linked list,

    void createLL() {
            head = new Node(10);
            head.next = new Node(20);
            head.next.next = new Node(30);
            head.next.next.next = new Node(40);
        }

This can be represented using an image as below for easier understanding,

enter image description here So in the createLL method you have now created a linked list like


    10->20->30->40->null

here the first node which is also called as the "head node" contains the data 10.

Now inside the method printLL(), the first line is as below,

    Node node = head;

Here you created a temporary node named "node" and assigned it to value of head. The head contains the information of the next node and the next node contains the information of its next node and so on. So now using your temporary node "node" and assigning it the value of headnode, you can traverse the whole linked list you created via the method createLL().

And in the while loop below,

while (node != null) {
        System.out.println(node.data);
        node = node.next;
    }

You have now traversed the linked list one by one until it reached to null. Remember the last node of a linked list always points to null. This while loop did not reassign the value/data of any nodes. But it just iterated from the first node till it reached the lastnode using a temporary node called "node". And the while loop finally assigns the "node" variable to null.

So in the if loop comparison,

    if (head.equals(node))

you are comparing,

if(10.equals(null)

which will return as false, since headnode still pointing to the 10 itself. only the temporary node "node" is pointing to the null which does not affect the headnode or any node of the linked list you have created.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...