I’m attempting to reverse a singly linked list in Java, but I'm running into confusion about how references work during the reversal process. Specifically, I don’t understand why setting the next pointer of a node to null doesn’t affect the node that it was pointing to (i.e., the node after it).
Here’s an example to illustrate the problem:
Example: Consider the following initial linked list:
1 -> 2 -> 3 -> null In the first iteration of the loop, node 1’s next points to node 2. I am using the following code to reverse the list:
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode temp = head; while (temp != null) { ListNode front = temp.next; // Save next node (which is node 2 initially) temp.next = prev; // Set node1.next to null (disconnect node1 from node2) prev = temp; temp = front; } return prev; } } What I expect to happen: Initially, node 1's next points to node 2. In the first iteration, when I set node1.next = null, I expect node 2 itself to somehow be affected — either its next pointer should become null, or node 2 itself should become null. What actually happens: After setting node1.next = null, the list becomes:
1 -> null 2 -> 3 -> null Node 2 remains unaffected, and its next pointer is still pointing to node 3. Node 2 itself is not nullified.
My confusion: Why doesn’t node 2 become null when I set node 1’s next pointer to null? I expected that disconnecting node 1 from node 2 would also somehow affect node 2 (perhaps by nullifying its next or even nullifying the node itself), but this doesn’t happen.
Example with Front Pointer: Let me explain this confusion with a related example:
When I perform front = temp.next, this saves the reference to the next node (i.e., in the first iteration, front will be pointing to node 2 because temp is node 1). This is valid, and node 2 is now the front reference.
However, when I perform temp.next = prev; (i.e., setting node1.next = null), only node 1’s next pointer is updated. It doesn’t affect node 2, even though in the front = temp.next step, node 1’s next was pointing to node 2.
Why is it that when I set node1.next = null, only node 1’s next pointer is updated, but not node 2’s next (or node 2 itself)?
What I don’t understand: Why doesn’t setting node1.next = null also affect the node that node1.next was pointing to (i.e., node 2)? If front can correctly hold a reference to node 2 when saving temp.next, why doesn’t node 2 itself become null (or its next pointer be modified) when setting node1.next = null?