-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedlist.java
351 lines (279 loc) · 6.3 KB
/
Linkedlist.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
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
package com.sai;
import java.util.HashSet;
public class Linkedlist {
ListNode head = null;
ListNode tail = null;
int size = 0;
public void addStart(int data) {
ListNode newNode = new ListNode(data);
if (head == null) {
head = newNode;
tail = newNode;
size++;
} else {
newNode.next = head;
head = newNode;
size++;
}
}
public void addEnd(int data) {
ListNode newNode = new ListNode(data);
if (tail == null) {
head = newNode;
tail = newNode;
size++;
} else {
tail.next = newNode;
tail = newNode;
size++;
}
}
public int addAtIndex(int data, int index) {
if (index > size + 1 || index < 1) {
System.out.println("Invalid position");
}
ListNode before = null;
ListNode after = null;
ListNode dummy = head;
int count = 0;
while (dummy.next != null) {
count++;
if (count == index - 1) {
before = dummy;
}
dummy = dummy.next;
}
after = before.next;
ListNode node = new ListNode(data);
before.next = node;
node.next = after;
size++;
return data;
}
public int Delete(int data) {
ListNode start = head;
ListNode before = null;
while (start.data != data) {
if (start.next == null) {
System.out.println("Not existing");
} else {
before = start;
start = start.next;
}
}
if (start == head) {
head = head.next;
size--;
} else {
before.next = start.next;
size--;
}
return start.data;
}
public int find(int val) {
ListNode finder = head;
while (finder.data != val) {
if (finder.next == null) {
return 0;
} else {
finder = finder.next;
}
}
return finder.data;
}
public void display() {
ListNode dummy = head;
while (dummy != null) {
System.out.println(dummy);
dummy = dummy.next;
}
}
public void displayReverse(ListNode head) {
ListNode dummy = head;
while (dummy != null) {
System.out.println(dummy.data);
dummy = dummy.next;
}
}
public void ReverseList() {
ListNode current = head;
ListNode previous = null;
ListNode next = null;
while (current != null) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
System.out.println("After Reversing");
displayReverse(previous);
}
public int Addafter(int data, int val) {
ListNode dummy = head;
ListNode previous = null;
ListNode newNode = new ListNode(val);
if (head.data == data) {
newNode.next = head.next;
head.next = newNode;
} else {
while (dummy.data != data) {
dummy = dummy.next;
previous = dummy;
}
newNode.next = previous.next;
previous.next = newNode;
size++;
}
return val;
}
public int printNthFromLast(int n) {
ListNode dummy = head;
int len = 0;
while (dummy != null) {
len++;
dummy = dummy.next;
}
if (n > len) {
return -1;
}
dummy = head;
for (int i = 1; i < len - n + 1; i++) {
dummy = dummy.next;
}
return dummy.data;
}
public int printNthFromLastRecursion(ListNode node, int n) {
if (node == null) {
return 0;
}
int index = printNthFromLastRecursion(node.next, n) + 1;
if (index == n) {
System.out.println(n + "th node from last is " + node.data);
}
return index;
}
ListNode nthTolast(ListNode head, int k) {
ListNode pl = head;
ListNode p2 = head;
/* Move pl k nodes into the list. */
for (int i = 0; i < k; i++) {
if (pl == null)
return null; // Out of bounds
pl = pl.next;
}
/*
* Move them at the same pace. When pl hits the end, p2 will be at the
* right element.
*/
while (pl != null) {
pl = pl.next;
p2 = p2.next;
}
return p2;
}
public ListNode middlenumber() {
ListNode slowptr = head;
ListNode fastptr = head;
while (fastptr != null) {
fastptr = fastptr.next;
if (fastptr != null && fastptr.next != null) {
slowptr = slowptr.next;
fastptr = fastptr.next;
}
}
return slowptr;
}
public ListNode RevUsingRecursion(ListNode node) {
if (node == null || node.next == null) {
return node;
}
ListNode remaining = RevUsingRecursion(node.next);
node.next = node;
node.next = null;
System.out.println(remaining.data);
return remaining;
}
public boolean checkPalindrome() {
ListNode middle = middlenumber();
ListNode secondHead = middle.next;
middle.next = null;
ListNode revsecond = RevUsingRecursion(secondHead);
while (head != null && secondHead != null) {
if (head.data == revsecond.data) {
head = head.next;
revsecond = revsecond.next;
continue;
} else {
return false;
}
}
return true;
}
public void removeDuplicates() {
HashSet<Integer> set = new HashSet<Integer>();
ListNode dummy = head;
ListNode previous = null;
while (dummy != null) {
if (set.contains(dummy.data)) {
previous.next = dummy.next;
} else {
set.add(dummy.data);
previous = dummy;
}
dummy = dummy.next;
}
}
public static void main(String[] args) {
Linkedlist list = new Linkedlist();
list.addEnd(4);
list.addEnd(5);
list.addEnd(4);
list.addEnd(7);
list.addEnd(7);
list.addStart(2);
System.out.println("before insertion");
list.display();
int z = list.addAtIndex(3, 2);
System.out.println("after insertion of " + z);
list.display();
int x = list.Delete(3);
System.out.println("after deletion of " + x);
list.display();
int y = list.find(5);
if (x != 0) {
System.out.println("Found " + y);
} else {
System.out.println("Not Found");
}
int m = list.Addafter(2, 3);
System.out.println("After adding " + m);
list.display();
System.out.println("2nd element from end is " +list.printNthFromLast(2));
list.printNthFromLastRecursion(list.head, 4);
System.out.println("3rd element from end is " +list.nthTolast(list.head, 3).data);
// boolean bool =list.checkPalindrome();
// if(bool){
// System.out.println("List is palindrome");
// }else{
// System.out.println("List is not palindrome");
// }
System.out.println("Middle element is " + list.middlenumber().data);
// System.out.println("Reversing using recursion");
// list.RevUsingRecursion(list.head);
System.out.println("After removing duplicates");
list.removeDuplicates();
list.display();
//list.ReverseList();
list.RevUsingRecursion(list.head);
}
}
class ListNode {
int data;
ListNode next;
ListNode(int data) {
this.data = data;
}
public String toString() {
return data + "";
}
}