-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathLinkedList.java
More file actions
278 lines (248 loc) · 5.83 KB
/
LinkedList.java
File metadata and controls
278 lines (248 loc) · 5.83 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
package Algorithms_Java;
public class LinkedList {
public class Node {
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public LinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
private void handleOnlyAddition(int data) {
Node newnode = new Node();
newnode.data = data;
newnode.next = null;
this.head = this.tail = newnode;
// this.tail = newnode;
this.size++;
}
private int handleOnlyItemRemoval() {
Node nodetoberemoved = this.head;
this.head = this.tail = null;
this.size--;
return nodetoberemoved.data;
}
public void addFirst(int data) {
if (this.size() == 0) {
handleOnlyAddition(data);
return;
}
Node newnode = new Node();
newnode.data = data;
newnode.next = this.head;
this.head = newnode;
this.size++;
}
public void AddAt(int index, int data) throws Exception {
if (index < 0 || index > this.size()) {
throw new Exception("Index out of bounds");
} else if (index == 0) {
this.addFirst(data);
} else if (index == this.size()) {
this.addLast(data);
} else {
Node node = new Node();
node.data = data;
Node prevnode = this.getNodeAt(index - 1);
node.next = prevnode.next;
prevnode.next = node;
this.size++;
}
}
public void addLast(int data) {
if (this.size() == 0) {
handleOnlyAddition(data);
} else {
Node newnode = new Node();
newnode.data = data;
newnode.next = null;
this.tail.next = newnode;
this.tail = newnode;
this.size++;
}
}
public int getFirst() throws Exception {
if (this.size() == 0) {
throw new Exception("Linked list is empty.");
}
return this.head.data;
}
public int getValueAt(int index) throws Exception {
if (index < 0 || index >= this.size()) {
throw new Exception("Index out of bounds.");
}
Node node = this.head;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node.data;
}
public Node getNodeAt(int index) throws Exception {
if (index < 0 || index >= this.size()) {
throw new Exception("Index out of bounds.");
}
Node node = this.head;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
}
public int getLast() throws Exception {
if (this.size() == 0) {
throw new Exception("Linked list is empty.");
}
return this.tail.data;
}
public int removeFirst() throws Exception {
if (this.size() == 0) {
throw new Exception("Linked list is empty.");
}
if (this.size() == 1) {
int rv = handleOnlyItemRemoval();
return rv;
}
Node nodetoberemoved = this.head;
this.head = nodetoberemoved.next;
this.size--;
return nodetoberemoved.data;
}
public int removeAt(int index) throws Exception {
if (index < 0 || index >= this.size()) {
throw new Exception("Index out of bounds");
} else if (index == 0) {
return this.removeFirst();
} else if (index == this.size() - 1) {
return this.removeLast();
} else {
Node prevnode = this.getNodeAt(index - 1);
Node nextnode = prevnode.next;
prevnode.next = nextnode.next;
this.size--;
return prevnode.data;
}
}
public int removeLast() throws Exception {
if (this.size() == 0) {
throw new Exception("Linked list is empty.");
}
if (this.size() == 1) {
int rv = handleOnlyItemRemoval();
return rv;
}
Node nodetoberemoved = this.tail;
Node prevnode = this.getNodeAt(this.size() - 2);
prevnode.next = null;
this.size--;
return nodetoberemoved.data;
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return this.size() == 0;
}
public void display() {
for (Node node = this.head; node != null; node = node.next) {
System.out.print(node.data + "->");
}
System.out.println(".");
}
public void displayReverse() {
this.displayReverseHelper(this.head);
}
private void displayReverseHelper(Node node) {
if (node.next == null) {
System.out.println(node.data);
return;
}
displayReverseHelper(node.next);
System.out.println(node.data);
}
public void reverseDataIterative() throws Exception {
Node left = this.head;
Node right = this.tail;
int li = 0;
int ri = this.size() - 1;
while (li <= ri) {
int temp = left.data;
left.data = right.data;
right.data = temp;
left = left.next;
right = this.getNodeAt(ri - 1);
li++;
ri--;
}
}
public void reversePointerIterative() {
Node prevnode = this.head;
Node currnode = prevnode.next;
while (currnode != null) {
Node tempnext = currnode.next;
currnode.next = prevnode;
prevnode = currnode;
currnode = tempnext;
}
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
this.tail.next = null;
}
public void reversePointerRecursive() {
rprHelper(this.head);
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
this.tail.next = null;
}
private void rprHelper(Node node) {
if (node.next == null) {
return;
}
rprHelper(node.next);
node.next.next = node;
}
public class HeapMover {
Node node;
}
public void reverseDataRecursive() {
HeapMover left = new HeapMover();
left.node = this.head;
Node right = this.head;
rdrHelper(left, right, 0);
}
private void rdrHelper(HeapMover left, Node right, int floor) {
if (right == null) {
return;
}
rdrHelper(left, right.next, floor + 1);
if (floor >= this.size() / 2) {
int temp = left.node.data;
left.node.data = right.data;
right.data = temp;
}
left.node = left.node.next;
}
public void fold() {
HeapMover left = new HeapMover();
left.node = this.head;
Node right = this.head;
foldHelper(left, right, 0);
}
private void foldHelper(HeapMover left,Node right,int floor) {
foldHelper(left, right.next, floor + 1);
if (floor >= this.size() / 2) {
Node temp = left.node.next;
left.node.next = right;
right.next = temp;
}
if(floor==this.size()/2) {
this.tail=right;
this.tail.next=null;
}
left.node=left.node.next.next;
}
}