简单链表操作
判断重复用hash
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
if (head.next == null) return head;
Set<Integer> dupli = new HashSet<Integer>();
ListNode parent = head, child = head.next;
dupli.add(parent.val);
while (child != null) {
if (dupli.contains(child.val)) {
parent.next = child.next;
} else {
dupli.add(child.val);
parent = child;
}
child = child.next;
}
return head;
}
}