0

I am trying to understand the difference between node1.next = node3 and node2 = node3. In the linked list, node1.next = node3 gets rid of node2. But node1.next points to node2 anyways so why doesn't node2 = node3 not work in the following code?

public class LinkedList {
    LinkedList head = null;
    LinkedList next = null;
    int data = 0;

    public static void main(String[] args) {
        LinkedList node1 = new LinkedList();
        LinkedList node2 = new LinkedList();
        LinkedList node3 = new LinkedList();
        node1.data = 1;
        node1.next = node2;
        node2.data = 2;
        node2.next = node3;
        node3.data = 3;
        node3.next = null;

        node2 = node3;// If I replace with node1.next = node3; it works
        LinkedList h = node1;
        while (h.next != null) {
            System.out.println(h.data);
            h = h.next;
        }
    }
}
t0mppa
  • 3,983
  • 5
  • 37
  • 48

3 Answers3

0

node1.next = node3 says that node1.next points to node3, thereby removing node2 from the list.

Whereas node2 = node 3 points node2 to node3; however, node1.next is still pointing to the original node2 (before it was reassigned to point to node3), thereby not removing node2.

The key takeaway is the following:

node1.next = node2;
node2 = null;

System.out.println(node1.next.data); 
//prints whatever node2's data value was before becoming null
Steve P.
  • 14,489
  • 8
  • 42
  • 72
0

executing the code

node2 = node3; 

doesn't change node1's reference to node2, or node2's reference to node3

this assignment statement only changes what's stored at the variable node2

after you do this, node1 can still see your original node2

ceptno
  • 687
  • 2
  • 6
  • 28
0
node1 // points to a structure containing:
  a
  b
  c
  next --> node2

node2 // points to a structure containing:
  a
  b
  c
  next --> node3

node3 // points to a structure containing:
  a
  b
  c
  next --> <null>

When you execute node2 = node3, you change what the node2 variable points to. That doesn't change anything in node1.

When you execute node1.next = node3, you change what the next part of node1 points to.


In an attempt to make it 100% clear, after a comment saying this was not.

We have three variables, node1, node2 and node3. Each of these resides in memory at some particular address, let us say 10, 20, and 30.

When an object is allocated, there is a corresponding block of memory at a particular address; let us say the objects of node1, node2, and node3 have had blocks allocated at 100, 200, and 300.

To say that node1 "has the value of" its object, what is happening at the implementation level is that the memory at the address of the variable contains the address of the allocated object. In our example, the memory at 10 has the value of 100, 20 has the value 200, 30 has the value 300.

The block of memory starting at 100, pointed to by node1, has a field in it that contains a pointer to another such object; the value of that field, in our example, would be 200.

When we execute node2 = node3, we would put the value 300 into the memory address 20. The memory at 20 now contains 300, where it used to contain 200. But all three objects still exist and are still reachable, and were not changed by this operation. So the node1.next value is still 200.

I hope this makes it 100% clear. If not, let us know, I have one more example in mind that the margin of my web page is too small to contain...

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Still not 100% clear. node2 points to node3. Why doesn't the node2 present in the linkedlist not update itself? – fidgetyPhil Dec 27 '13 at 01:34
  • ok. Thanks for the clarification. I think I finally got it. Imagine 3 houses (objects). node1, node2, node3 are three address cards. And inside each house there is a blank address card called next which tells us which house to visit next. When node1.next = node2 we are copying the location of object2 represented by node2 onto the next address card. Later, even if we change the address on node2, it doesn't matter because we already copied the original address on it to the node1.next – fidgetyPhil Dec 27 '13 at 11:06
  • I'll have to remember the index cards -- probably better for novice programmers than memory addresses... Feel free to vote this and any other answer 'up' if you find it useful, it's the way SO works... – arcy Dec 27 '13 at 19:30