-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
249 lines (230 loc) · 7.29 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
package com.webaddicted.jetpackcomposedemo;
import android.util.Log;
class LinkedList {
Node head;
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public static void test() {
LinkedList test = new LinkedList();
test.insert(2);
test.insert(3);
test.insert(4);
test.insertFirst(1);
test.insertLast(5);
test.getLength();
test.findItem(5);
// test.reverseInPair();
test.findMidElement();
// test.reverse();
// test.reverseLeftAndRightFromMid();
test.printNthFromLast(test.head, 2);
}
void reverseLeftAndRightFromMid() {
Node midNode = head;
Node fastPtr = head;
while (fastPtr != null && fastPtr.next != null) {
fastPtr = fastPtr.next.next;
midNode = midNode.next;
}
Log.d("TAG", "Mid Node : " + midNode.data);
Node pre = null;
Node current = head;
Node next = null;
while (current != null && current != midNode) {
next = current.next;
current.next = pre;
pre = current;
current = next;
}
Node leftReverseLinkedList = pre;
Log.d("TAG", "Reverse Left Node From Mid : ");
display(leftReverseLinkedList);
pre = null;
current = midNode;
next = null;
while (midNode != null) {
next = midNode.next;
midNode.next = pre;
pre = midNode;
midNode = next;
}
Node rightReverseLinkedList = pre;
Log.d("TAG", "Reverse Right Node From Mid : ");
display(rightReverseLinkedList);
Node finalLinkedList = null;
// while (leftReverseLinkedList.next != null) {
// if (finalLinkedList == null) {
// finalLinkedList = new Node(leftReverseLinkedList.data);
// } else {
// finalLinkedList.next = new Node(leftReverseLinkedList.data);
// }
// leftReverseLinkedList = leftReverseLinkedList.next;
// }
// while (rightReverseLinkedList != null) {
// if (finalLinkedList == null) {
// finalLinkedList = new Node(rightReverseLinkedList.data);
// } else {
// finalLinkedList.next = new Node(rightReverseLinkedList.data);
// }
// rightReverseLinkedList = rightReverseLinkedList.next;
// }
display(finalLinkedList);
}
void insert(int data) {
Node node = new Node(data);
Node current = head;
if (current == null) {
head = node;
} else {
while (current != null) {
if (current.next == null) {
current.next = node;
break;
} else {
current = current.next;
}
}
}
Log.d("TAG", "Insert : " + data);
display();
}
void insertFirst(int data) {
Node node = new Node(data);
node.next = head;
head = node;
Log.d("TAG", "Insert First Pointer : " + data);
display();
}
void insertLast(int data) {
Node newNode = new Node(data);
Node current = head;
while (current != null) {
if (current.next == null) {
current.next = newNode;
break;
} else {
current = current.next;
}
}
Log.d("TAG", "Insert Last Pointer : " + data);
display();
}
void printNthFromLast(Node head, int n) {
Node pre = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next;
current.next = pre;
pre = current;
current = next;
}
current = pre;
for (int i = 1; i <= n; i++) {
if (i == n) {
Log.d("TAG", "Last " + n + " item is : " + current.data);
break;
} else {
current = current.next;
}
}
}
void getLength() {
Node current = head;
int count = 0;
while (current != null) {
current = current.next;
count++;
}
Log.d("TAG", "Size of LinkedList : " + count);
Log.d("TAG", ".");
}
void findItem(int data) {
Node current = head;
int index = 0;
boolean isElementFound = false;
while (current != null) {
index++;
if (current.data == data) {
isElementFound = true;
break;
} else {
current = current.next;
}
}
if (isElementFound) {
Log.d("TAG", ("Element : " + data) + " Found at index : " + index);
} else {
Log.d("TAG", ("Element : " + data) + " Not Found");
}
}
void findMidElement() {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
Log.d("TAG", "LinkedList Mid Element : " + slow.data);
Log.d("TAG", ".");
}
void reverseLinkedList() {
// 1>2>3>4
Node pre = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next;//2
current.next = pre;//null
pre = current;//1
current = next;//2
}
head = pre;
Log.d("TAG", "Reverse Linked List : ");
display();
// In this way we reverse pointer
// after every iteration current and next move at next position
// List - 1>2>3>4
// After 1st Iteration- null>1, 2>3>4
// After 2st Iteration- null<1<2, 3>4
// After 3st Iteration- null<1<2<3, 4
// After 4st Iteration- null<1<2<3<4
// finally O/P : 4>3>2>1>null
}
void reverseInPair() {
Node current = head;
while (current != null && current.next != null) {
// Swap Data in pair
int temp = current.data;
current.data = current.next.data;
current.next.data = temp;
current = current.next.next;
}
Log.d("TAG", "Reverse Data in Pair");
display();
}
void display(Node node) {
Node current = node;
while (current != null) {
Log.d("TAG", "Print LinkedList Item : " + current.data);
current = current.next;
}
Log.d("TAG", "Complete Iteration : \n\n");
Log.d("TAG", ".");
}
void display() {
Node current = head;
while (current != null) {
Log.d("TAG", "Print LinkedList Item : " + current.data);
current = current.next;
}
Log.d("TAG", "Complete Iteration : \n\n");
Log.d("TAG", ".");
}
}