-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
308 lines (256 loc) · 8.1 KB
/
LinkedList.java
File metadata and controls
308 lines (256 loc) · 8.1 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import java.util.HashSet;
public class LinkedList {
public static void main(String[] args) {
Node list = new Node(1);
list.append(2);
list.append(3);
list.append(4);
Node list2 = new Node(3);
list2.append(4);
list2.append(6);
System.out.println(getIntersection(list, list2).data);
}
//2.1
// O(N): N = number of nodes in list
// iterate through the list, adding each element to a hash set
// if we discover a duplicate, we remove the element (list assigns previous list the next node)
public static void removeDupes(Node list) {
java.util.HashSet<Integer> set = new java.util.HashSet<>();
Node previous = null;
while (list != null) {
if (set.add(list.data)) {
previous = list;
} else {
previous.next = list.next;
}
list = list.next;
}
}
//O(1) Space, O(n^2) time
public static void removeDupesRunner(Node head) {
Node current = head;
while (current != null) {
Node runner = current;
while (runner.next != null) {
if (runner.next.data == current.data) {
runner.next = runner.next.next;
} else {
runner = runner.next;
}
}
current = current.next;
}
}
//2.2
// recursive O(N)
// recurse until last element (list == null) then unwind
// during that unwinding, if i == given index print it
// else increment i and return it
public static int nthToLastRecursive(Node list, int index) {
if (list == null) {
return 0;
}
int i = nthToLastRecursive(list.next, index) + 1;
if (i == index) {
System.out.println(list.data);
}
return i;
}
//2.2
//iterative O(N) time and O(1) space
//using two pointers p1 is at head, p2 is {{index}} away from head
//once p2 reaches the end p1 will be {{index}} from the tail.
public static int nthToLastIterative(Node list, int index) {
if (index < 0) return -1;
Node p1 = list;
Node p2 = list;
for (int i = 0; i < index - 1; i++) {
if (p2 == null) return -1;
p2 = p2.next;
}
while (p2.next != null) {
p1 = p1.next;
p2 = p2.next;
}
return p1.data;
}
//2.2 my own simple solution
public static int kthToLast(Node list, int k) {
int length = 0;
Node temp = list;
while (temp != null) {
length++;
temp = temp.next;
}
for (int i = 0; i < (length - k); i++) {
list = list.next;
}
return list.data;
}
//2.3
//Only given node to delete
//copy next to current
public static boolean deleteNodeAt(Node n) {
if (n == null || n.next == null) return false;
Node next = n.next;
n.next = next.next;
n.data = next.data;
return true;
}
//2.4
//iterator through the list and insert into appropriate new list
//once finished, merge
public static Node partitionList(Node list, int value) {
Node beforeList = null;
Node afterList = null;
//partition list
while (list != null) {
Node next = list.next; //store temp since modifying list as we iter
if (list.data < value) {
// insert node into end of before list
list.next = beforeList;
beforeList = list;
} else {
list.next = afterList;
afterList = list;
}
list = next;
}
if (beforeList == null) return afterList;
Node head = beforeList;
while (beforeList.next != null) {
beforeList = beforeList.next;
}
beforeList.next = afterList;
return head;
}
//2.5 my own
public static Node listAdd(Node num1, Node num2) {
checkLargerLength(num1, num2);
Node sum = new Node(-1);
while (num1 != null) {
int value = num1.data + num2.data;
if (value >= 10) {
int remainder = value % 10;
int carry = (value - remainder) / 10;
num1.next.data += carry;
value = remainder;
}
if (sum.data == -1) {
sum.data = value;
} else {
sum.append(value);
}
num1 = num1.next;
num2 = num2.next;
}
return sum;
}
public static void checkLargerLength(Node list1, Node list2) {
if (list1.length > list2.length)
appendZeros(list2, (list1.length - list2.length));
else if (list2.length > list1.length)
appendZeros(list1, (list2.length - list1.length));
}
public static void appendZeros(Node list, int difference) {
for (int i = 0; i < difference; i++) {
list.append(0);
}
}
//2.6 - detected beginning of loop in a list
//move fastPointer at 2k and slowPointer at k pace
//when they collide, move slowPointer to the head. keep fastPointer
//move pointers at k each, new collide is loop start.
public static Node findFirstInLoop(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
//collision
break;
}
}
//check if no meeting point (no loop)
if (fast == null || fast.next == null) {
return null;
}
//move slow back to head
slow = head;
//loop until collide taking 1 step each
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
//2.7 - check if list is a palindrome
//use fast/slow runner technique to find middle
//check if first half matches second half
public static boolean isPalindrome(Node head) {
Node fast = head;
Node slow = head;
java.util.Stack<Integer> stack = new java.util.Stack<>();
while (fast != null && fast.next != null) {
stack.push(slow.data);
slow = slow.next;
fast = fast.next.next;
}
//has odd number of elements
if (fast != null) {
slow = slow.next;
}
while (slow != null) {
int top = stack.pop();
if (top != slow.data) {
return false;
}
slow = slow.next;
}
return true;
}
public static boolean isPal(Node head) {
Node rev = head.reverse();
while (head != null) {
if (head.data != rev.data) return false;
head = head.next;
rev = rev.next;
}
return true;
}
public static Node getIntersection(Node list1, Node list2) {
HashSet<Integer> set = new HashSet<>();
while (list1 != null) {
set.add(list1.data);
list1 = list1.next;
}
while (list2 != null) {
if (!set.add(list2.data)) return list2;
else list2 = list2.next;
}
return null;
}
//delete node
public boolean delete(Node head, int searchValue) {
Node tmpNode = head;
Node prevNode = null;
boolean deletedANode = false;
while (tmpNode != null) {
if (tmpNode.data == searchValue) {
if (tmpNode == head) {
head = head.next;
} else { // fixed indenting/newline
prevNode.next = tmpNode.next;
}
// fixed indenting
deletedANode = true;
} else {
// only advance the prevNode when there's no match.
prevNode = tmpNode;
}
tmpNode = tmpNode.next;
}
return deletedANode;
}
}