Skip to content

Commit 1d87fb3

Browse files
committed
add 面试题02-I for Java
1 parent 58b9c1b commit 1d87fb3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode removeDuplicateNodes(ListNode head) {
11+
if (head == null) {
12+
return null;
13+
}
14+
Set<Integer> cache = new HashSet<>();
15+
// 初始化参数
16+
ListNode pre = head, cur = pre.next;
17+
cache.add(head.val);
18+
19+
while (cur != null) {
20+
if (cache.contains(cur.val)) {
21+
pre.next = cur.next;
22+
cur = pre.next;
23+
} else {
24+
cache.add(cur.val);
25+
pre = cur;
26+
cur = cur.next;
27+
}
28+
}
29+
30+
return head;
31+
}
32+
}

0 commit comments

Comments
 (0)