-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomLinkedList.java
More file actions
393 lines (338 loc) · 10.4 KB
/
CustomLinkedList.java
File metadata and controls
393 lines (338 loc) · 10.4 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package com.company;
import java.util.*;
public class CustomLinkedList {
public static Node recursiveReverse(Node node){
if(node.next == null){
return node;
}
Node reversedList = recursiveReverse(node.next);
reversedList.printList();
//The last node points back to the previous node in the stack
node.next.next = node;
//Nullifies the forward pointers in the stack
node.next = null;
return reversedList;
}
//Given an unsorted linked list, remove the duplicates
public static Node remove_duplicates(Node head){
if(head == null || head.next == null){
return null;
}
HashSet<Integer> nodes = new HashSet<Integer>();
Node previous = null;
while(head != null){
if(nodes.contains(head.data)){
//skip the node that contains the duplicate
previous.next = head.next;
}
else{
nodes.add(head.data);
previous = head;
}
head = head.next;
}
return previous;
}
public static Node partition(Node head, int n){
Node left = new Node(0);
Node right = new Node(0);
Node rightHead = right;
Node leftHead = left;
while(head != null){
if(head.data < n){
left.next = head;
left = left.next;
}
else{
right.next = head;
right = right.next;
}
head = head.next;
}
right.next = null;
left.next = rightHead.next;
return leftHead.next;
}
//Find the kth to the last element in a singly linked list
public static int getKthfromLastNode(Node head, int k){
if(head == null){
return -1;
}
int length = head.length();
if(k > length || k < 0 ){
System.out.println("Out of Range");
return -1;
}
int position = length - k;
while(position > 0){
head = head.next;
position--;
}
return head.data;
}
//Cracking the Coding Interview Solution
public static int printKthToLast(Node head, int k){
if(head == null){
return 0;
}
int index = printKthToLast(head.next, k) +1;
if(index == k){
System.out.println(k + "th to last node is " + head.data);
}
return index;
}
public static Node detectLoop(Node head){
HashSet<Node> nodes = new HashSet<>();
while(head != null){
if(nodes.contains(head)){
return head;
}
nodes.add(head);
head = head.next;
}
return null;
}
//Given an sorted linked list, remove the duplicates
public static Node remove_sorted_duplicates(Node head){
if(head.next == null || head == null) return null;
Node prev = head;
head = head.next;
while(head != null){
if(prev.data == head.data){
prev.next = head.next;
}
prev = head;
head = head.next;
}
return prev;
}
public static Node reverse(Node head){
Node nextNode = null;
Node prev = null;
Node curr = head;
while(curr != null){
//Temp pointer to the next node
//Necessary because current link to the next node will be altered
nextNode = curr.next;
//Breaks forward link and redirects to point to the previous node
curr.next = prev;
//Previous points to the next node in the linked list
prev = curr;
//Current points to the nextNode
//Equivalent to curr = curr.next, but since curr.next was altered we use nextNode
curr = nextNode;
}
//prev is now the new head
return prev;
}
public static Node Intersection(Node a, Node b){
Node headPtr = a;
while(a != null && b != null){
if(a.next == b.next){
return a.next;
}
if(a.next == null){
a = headPtr;
b = b.next;
}
a = a.next;
}
return null;
}
public static Node addLists(Node a, Node b){
int sum = getStackSum(a) + getStackSum(b);
System.out.println(getStackSum(a));
Node addedList = intToList(sum);
return addedList;
}
public static Node intToList(int num){
char[] numChars = (Integer.toString(num)).toCharArray();
Node list = new Node(Character.getNumericValue(numChars[numChars.length-1]));
Node head = list;
for(int i = numChars.length-2; i >=0; i--){
list.next = new Node(Character.getNumericValue(numChars[i]));
list = list.next;
}
return head;
}
public static int getStackSum(Node head){
String numString = "";
Stack<String> nodes = new Stack<>();
while(head != null){
nodes.push(Integer.toString(head.data));
head = head.next;
}
while(!nodes.empty()){
numString += nodes.pop();
}
int num = Integer.parseInt(numString);
return num;
}
public static boolean isPalindrome(Node head){
Node headPtr = head;
Stack<Integer> nodes = new Stack<>();
while(head != null){
nodes.push(head.data);
head = head.next;
}
while(headPtr != null){
if(headPtr.data != nodes.peek()){
return false;
}
headPtr = headPtr.next;
nodes.pop();
}
return true;
}
public static class Node{
private int data;
private Node next;
public Node(int data){
this.data = data;
this.next = null;
}
public Node pop(){
Node node = this;
Node previous = node;
node = node.next;
while(node.next != null){
node = node.next;
previous = previous.next;
}
previous.next = null;
return previous;
}
public void deleteMiddleNode(){
Node head = this;
int length = (head.length()/2);
if(head.length() % 2 == 0){
length--;
}
System.out.println(length);
Node prev = head;
head = head.next;
length--;
System.out.println(length);
while(prev != null){
if(length == 0){
prev.next = head.next;
break;
}
prev = prev.next;
head = head.next;
length--;
}
}
public Node insert( int data, int position){
Node newNode = new Node(data);
Node currentObject = this;
if(position > currentObject.length() || position <= 0){
System.out.println("Invalid position");
return currentObject;
}
//Edge case isn't working for some reason
if(position == 1){
newNode.next = currentObject;
return newNode;
}
else {
int count = 1;
Node previous = null;
while (currentObject != null) {
if (count == position) {
previous.next = newNode;
newNode.next = currentObject;
}
previous = currentObject;
currentObject = currentObject.next;
count++;
}
return previous;
}
}
public Node delete(int data){
Node currentObject = this;
Node previous = new Node(currentObject.data);
currentObject = currentObject.next;
if(currentObject.length() == 1 && currentObject.data == data){
return null;
}
//Edge case isn't working for some reason
if(previous.data == data){
return currentObject;
}
while(currentObject != null){
if(currentObject.data == data){
previous.next = currentObject.next;
currentObject = previous.next;
}
else {
previous = currentObject;
currentObject = currentObject.next;
}
}
return previous;
}
public int length(){
int count = 0;
Node list = this;
while(list != null){
list = list.next;
count++;
}
return count;
}
public Node append(int data){
Node temp = new Node(data);
Node currentObj = this;
while(currentObj.next != null){
currentObj = currentObj.next;
}
currentObj.next = temp;
return temp;
}
public void printList(){
Node list = this;
while(list != null){
System.out.print(list.data);
if(list.next != null) {
System.out.print("->");
}
list = list.next;
}
System.out.println();
}
public int getData(){
return this.data;
}
public void setNext(Node next){
this.next = next;
}
}
public static void main(String[] args) {
//Driver Code
Node list = new Node(1);
list.append(2);
list.append(1);
//list.append(9000);
list.append(3);
list.append(4);
list.append(5);
// list.append(5);
// list.append(7);
// list.append(1);
// list.append(3);
// list.append(6);
// list.append(6);
// list.append(8);
// list.append(9);
// list.append(10);
// list.append(11);
// list.append(12);
// Node listTwo = new Node(1);
// Node a = list.append(5);
// listTwo.setNext(a);
// Node intersectingNode = Intersection(list, listTwo);
// intersectingNode.printList();
}
}