forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1836.java
29 lines (26 loc) · 848 Bytes
/
_1836.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import java.util.HashMap;
import java.util.Map;
public class _1836 {
public static class Solution1 {
public ListNode deleteDuplicatesUnsorted(ListNode head) {
Map<Integer, Integer> map = new HashMap<>();
ListNode tmp = head;
while (tmp != null) {
map.put(tmp.val, map.getOrDefault(tmp.val, 0) + 1);
tmp = tmp.next;
}
ListNode pre = new ListNode(-1);
tmp = pre;
while (head != null) {
if (map.get(head.val) == 1) {
tmp.next = new ListNode(head.val);
tmp = tmp.next;
}
head = head.next;
}
return pre.next;
}
}
}