hash(O(n))
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
int[] hash = new int[20005];
ListNode prev = head, curr = head;
while (curr != null) {
if (hash[curr.val] == 0) {
hash[curr.val] ++;
prev = curr;
} else {
prev.next = curr.next;
}
curr = curr.next;
}
return head;
}
}