In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.
Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?
Note: I know this code doesnt handle edge cases. like null nodes, etc...
public class LLnode{
int value;
LLnode next;
public LLnode(int x){
this.value = x;
this.next = NULL;
}
}
/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n){
LLnode front = head;
while (front.next.value != n){
front = front.next; //why DOESN'T this physically change the LL?
}
front.next = front.next.next; //why DOES this physically change the LL ?
}
public static void main(String[] args){
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);
//assignments
c.next = d;
b.next = c;
a.next = b;
removeNode(a,30);
}
Thanks.