In the Cracking the Coding Interview Linked List question: Write code to remove duplicates from an unsorted linked list, the solution is
public static void deleteDups (LinkedListNode n){
Hashtable table = new Hashtable();
LinkedListNode previous = null;
while(n!=null){
if(table.containsKey(n.data)){
previous.next = n.next;
} else {
table.put(n.data, true);
previous = n;
}
n = n.next;
}
}
My question is why does n = n.next
not alter the linked list passed into the function, but doing previous.next = n.next
and previous = n
do alter the linked list passed in?