通过万岁!!!
java代码
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if (head == null) {
return head;
}
ListNode root = head;
ListNode pre = head.next;
Set<Integer> set = new HashSet<>();
set.add(head.val);
while (pre != null) {
while (pre != null && set.contains(pre.val)) {
pre = pre.next;
}
if (pre == null) {
head.next = null;
break;
}
set.add(pre.val);
head.next = pre;
pre = pre.next;
head = head.next;
}
return root;
}
}