I am reading Cracking the Code Interview and I am getting a bit confused at the bit using 'previous'. In this problem, the method deletes duplicate nodes in a Linked List.
My assumption is that head
is used to scan through the linked list while previous is used to update the linked list head
because previous
points to the same linked list. Not sure if my assumption is correct. Could someone verify this please?
Perhaps someone could also provide a method of increasing the efficiency or space of this program? I would love your input!
public static void deleteDups1(LinkedListNode head) {
Hashtable<Integer, Boolean> table = new Hashtable<Integer, Boolean>();
LinkedListNode previous = null;
while(head != null) {
if (table.containsKey(head.value)) previous.next = head.next;
else {
table.put(head.value, true);
previous = head;
}
head = head.next;
}
}