-
Notifications
You must be signed in to change notification settings - Fork 94
/
RemoveDuplicates.java
65 lines (61 loc) · 1.34 KB
/
RemoveDuplicates.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package chapter02LinkedList;
import java.util.HashSet;
import java.util.Set;
/**
*
* Problem: Write code to remove duplicates from an unsorted linked list. 1 -> 2
* -> 1 ===> 1 -> 2
*
*/
public class RemoveDuplicates {
/**
* method 1: with temporary buffer.
*/
public ListNode deleteDups(ListNode node) {
if (node == null) {
return null;
}
ListNode head = node;
Set<Integer> set = new HashSet<>();
while (node.next != null) {
if (set.contains(node.next.val)) {
node.next = node.next.next;
} else {
set.add(node.val);
node = node.next;
}
}
return head;
}
/**
* method 2: without temporary buffer. two pointers. time O(N2), space O(1)
*/
public ListNode deleteDups2(ListNode node) {
ListNode head = node;
while (node != null) {
ListNode runner = node;
while (runner.next != null) {
if (runner.next.val == node.val) {
runner.next = runner.next.next;
} else {
runner = runner.next;
}
}
node = node.next;
}
return head;
}
public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(1);
n1.next = n2;
n2.next = n3;
RemoveDuplicates rmd = new RemoveDuplicates();
ListNode node = rmd.deleteDups(n1);
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
}
}