删除重复节点

题目

https://leetcode-cn.com/problems/remove-duplicate-node-lcci/

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if (head == null) {
return null;
}
Set<Integer> set = new HashSet<>();
ListNode p = head.next;
ListNode pre = head;
set.add(head.val);
while(p != null) {
if (!set.contains(p.val)) {
set.add(p.val);
pre = p;
p = p.next;
} else {
pre.next = p.next;
p = p.next;
}
}
return head;
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×