-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.java
263 lines (235 loc) · 6.18 KB
/
DoublyLinkedList.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
package gfg.ds.linked_list;
import gfg.ds.linked_list.adt.LinkedList;
import gfg.ds.tree.binary_search_tree.BinarySearchTree;
import gfg.ds.tree.binary_tree.BinaryTree;
import utils.Pointer;
import java.util.Objects;
/** @noinspection WeakerAccess */
public class DoublyLinkedList implements LinkedList {
private DNode head;
public int size;
@Override
public int size() {
return size;
}
/** t=O(1) */
@Override
public DoublyLinkedList insertAtFront(int data) {
DNode node = new DNode(data);
node.next = this.head;
if (this.head != null) {
this.head.prev = node;
}
this.head = node;
size++;
return this;
}
/** t=O(n) */
@Override
public DoublyLinkedList insertAtPos(int pos, int data) {
assert pos >= 0 && pos <= size : String.format("Position should be between %s and %s", 0, size);
if (pos == 0) {
insertAtFront(data);
return this;
}
if (pos == size) {
insertAtEnd(data);
return this;
}
DNode temp = this.head;
// No need of prev as we know have prev pointer.
for (int i = 0; i < pos; i++) {
temp = temp.next;
}
DNode node = new DNode(data);
node.prev = temp.prev;
temp.prev = node;
node.next = temp;
node.prev.next = node;
size++;
return this;
}
/** t=O(1) */
@Override
public DoublyLinkedList insertAtEnd(int data) {
if (this.head == null) {
this.head = new DNode(data);
size++;
return this;
}
DNode temp;
for (temp = this.head; temp.next != null; ) {
temp = temp.next;
}
temp.next = new DNode(data);
temp.next.prev = temp;
size++;
return this;
}
/** t=O(n) */
public DoublyLinkedList delete(int data) {
DNode curr = this.head;
for (; curr != null && curr.data != data; ) {
curr = curr.next;
}
assert curr != null : "Node with given data not found";
if (curr.next != null) {
curr.next.prev = curr.prev;
}
if (curr.prev != null) {
curr.prev.next = curr.next;
} else {
this.head = curr.next;
}
size--;
return this;
}
public DoublyLinkedList append(int... data) {
for (Integer item : data) {
insertAtEnd(item);
}
return this;
}
/** t=O(n) NOTE: modified the list(NOT PURE) */
public DoublyLinkedList reverse() {
if (head == null || size == 1) {
return this;
}
DNode prev = null;
DNode curr = head;
for (; curr != null; ) {
prev = curr.prev;
curr.prev = curr.next;
curr.next = prev;
curr = curr.prev;
}
this.head = prev.prev;
return this;
}
/** t = O(n * log n) in best and average case = O(n^2) in worst case (list is already sorted) */
public static void quickSort(DoublyLinkedList dll) {
if (dll == null || dll.size <= 1) {
return;
}
DNode last = dll.head;
while (last.next != null) {
last = last.next;
}
quickSortUtil(dll.head, last);
}
private static void quickSortUtil(DNode first, DNode last) {
if (first == null || last == null || first == last) {
return;
}
DNode pivot = partition(first, last);
quickSortUtil(first, pivot.prev);
quickSortUtil(pivot.next, last);
}
/** We are swapping data not links. */
private static DNode partition(DNode first, DNode last) {
DNode i = null;
DNode curr = first;
while (curr != last) {
if (curr.data >= last.data) {
curr = curr.next;
continue;
}
i = (i == null) ? first : i.next;
swapData(curr, i);
curr = curr.next;
}
i = (i == null) ? first : i.next;
swapData(last, i);
return i;
}
private static void swapData(DNode first, DNode second) {
int temp = first.data;
first.data = second.data;
second.data = temp;
}
/** If list is not sorted then it will give unexpected results. */
public static BinarySearchTree toBST(DoublyLinkedList dll) {
assert dll.head != null;
int n = dll.size();
return BinarySearchTree.fromBinaryTree(
new BinaryTree().insertAtRoot(convertUtil(new Pointer<>(dll.head), n)));
}
/** t=O(n) create BST from leaves. */
private static BinaryTree.BinaryTreeNode convertUtil(Pointer<DNode> headPtr, int n) {
if (n == 0) {
return null;
}
BinaryTree.BinaryTreeNode left = convertUtil(headPtr, n / 2);
BinaryTree.BinaryTreeNode root = new BinaryTree.BinaryTreeNode(headPtr.data);
// headPtr is progressing as tree is constructing.
headPtr.data = headPtr.data.next;
BinaryTree.BinaryTreeNode right = convertUtil(headPtr, n - n / 2 - 1);
root.left = left;
root.right = right;
return root;
}
@Override
public String toString() {
if (this.head == null) {
return "{}";
}
DNode curr = this.head;
StringBuilder sb = new StringBuilder();
sb.append("{");
while (curr != null) {
sb.append(curr.data).append(",");
curr = curr.next;
}
sb.append("}");
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DoublyLinkedList that = (DoublyLinkedList) o;
if (size != that.size) return false;
if (size == 0) {
return true;
}
DNode thisCurr = head;
DNode thatCurr = that.head;
while (thisCurr != null) {
if (thisCurr.data != thatCurr.data) {
return false;
}
thisCurr = thisCurr.next;
thatCurr = thatCurr.next;
}
return true;
}
@Override
public int hashCode() {
int result = head != null ? head.hashCode() : 0;
result = 31 * result + size;
return result;
}
public static class DNode {
public DNode prev;
public DNode next;
public int data;
public DNode(int data) {
this.data = data;
}
@Override
public String toString() {
return "DNode{" + "prev=" + prev + ", next=" + next + ", data=" + data + "} ";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DNode dNode = (DNode) o;
return data == dNode.data;
}
@Override
public int hashCode() {
return Objects.hash(data);
}
}
}