We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 58b9c1b commit 1d87fb3Copy full SHA for 1d87fb3
lcof/面试题02-I.去除重复节点/Solution.java
@@ -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