-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRemoveDups.java
82 lines (72 loc) · 2.28 KB
/
RemoveDups.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package linkedlists;
import java.util.HashSet;
/**
* ZeroMatrix
* https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2002.%20Linked%20Lists/Q2_01_Remove_Dups
* Difficulty : Easy
* Related Topics : Linked List
*
* created by Cenk Canarslan on 2021-10-26
*/
public class RemoveDups {
public static void main(String[] args) {
ListNode listNode = new ListNode(5);
listNode.next = new ListNode(3);
listNode.next.next = new ListNode(8);
listNode.next.next.next = new ListNode(5);
listNode.next.next.next.next = new ListNode(9);
listNode.next.next.next.next.next = new ListNode(3);
printLinkedList(listNode);
// removeDuplicate(listNode);
removeDuplicateWithoutBuffer(listNode);
System.out.println("----------");
printLinkedList(listNode);
}
private static void removeDuplicate(ListNode listNode) {
if (listNode == null) {
return;
}
HashSet<Integer> set = new HashSet<>();
ListNode previous = null;
while (listNode != null) {
if (set.contains(listNode.val)) {
previous.next = listNode.next;
} else {
set.add(listNode.val);
previous = listNode;
}
listNode = listNode.next;
}
}
private static void removeDuplicateWithoutBuffer(ListNode listNode) {
ListNode curr = listNode;
while (curr != null) {
ListNode fasterNode = curr;
while (fasterNode.next != null) {
if (fasterNode.next.val == curr.val) {
fasterNode.next = fasterNode.next.next;
} else {
fasterNode = fasterNode.next;
}
}
curr = curr.next;
}
}
private static class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
private static void printLinkedList(ListNode listNode) {
while (listNode != null) {
System.out.println("listNode.val = " + listNode.val);
listNode = listNode.next;
}
}
}