forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
91 lines (83 loc) · 2.53 KB
/
Question.java
File metadata and controls
91 lines (83 loc) · 2.53 KB
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
83
84
85
86
87
88
89
90
91
package Question2_1;
import java.util.HashSet;
import java.util.Hashtable;
import CtCILibrary.AssortedMethods;
import CtCILibrary.LinkedListNode;
public class Question {
public static void deleteDupsA(LinkedListNode n) {
HashSet<Integer> set = new HashSet<Integer>();
LinkedListNode previous = null;
while (n != null) {
if (set.contains(n.data)) {
previous.next = n.next;
} else {
set.add(n.data);
previous = n;
}
n = n.next;
}
}
public static void deleteDupsC(LinkedListNode head) {
if (head == null) return;
LinkedListNode previous = head;
LinkedListNode current = previous.next;
while (current != null) {
// Look backwards for dups, and remove any that you see.
LinkedListNode runner = head;
while (runner != current) {
if (runner.data == current.data) {
LinkedListNode tmp = current.next;
previous.next = tmp;
current = tmp;
/* We know we can't have more than one dup preceding
* our element since it would have been removed
* earlier. */
break;
}
runner = runner.next;
}
/* If runner == current, then we didn't find any duplicate
* elements in the previous for loop. We then need to
* increment current.
* If runner != current, then we must have hit the ‘break’
* condition, in which case we found a dup and current has
* already been incremented.*/
if (runner == current) {
previous = current;
current = current.next;
}
}
}
public static void deleteDupsB(LinkedListNode head) {
if (head == null) return;
LinkedListNode current = head;
while (current != null) {
/* Remove all future nodes that have the same value */
LinkedListNode runner = current;
while (runner.next != null) {
if (runner.next.data == current.data) {
runner.next = runner.next.next;
} else {
runner = runner.next;
}
}
current = current.next;
}
}
public static void main(String[] args) {
LinkedListNode first = new LinkedListNode(0, null, null); //AssortedMethods.randomLinkedList(1000, 0, 2);
LinkedListNode head = first;
LinkedListNode second = first;
for (int i = 1; i < 8; i++) {
second = new LinkedListNode(i % 2, null, null);
first.setNext(second);
second.setPrevious(first);
first = second;
}
System.out.println(head.printForward());
LinkedListNode clone = head.clone();
deleteDupsA(head);
System.out.println(head.printForward());
deleteDupsC(clone);
}
}