Code from CCI book:
public class ListNode {
int val;
ListNode next = null;
ListNode(int x) {
this.val = x;
}
And this:
public void appendToTail(int d) {
ListNode end = new ListNode(d);
ListNode n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
}
Original: 5 -> 10 -> 15
As I understand, in this case "n" and "this" refer to same object yet, after debugging and stepping through:
"n" becomes 15 -> 20
"this" becomes 5 -> 10 -> 15 -> 20
How can this be? When I change "n", "this" should change as well?
So everything I did on "n" ,BESIDES appending 20, did not reflect on "this".
UPDATE:
Anyone who has same question should read this.