Repository navigation
Expand file tree
/
Copy pathLinkedList.java
More file actions
250 lines (213 loc) · 6.04 KB
/
Copy pathLinkedList.java
File metadata and controls
250 lines (213 loc) · 6.04 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
import java.util.*;
class Node {
String data;
Node next;
Node (String data) {
this.data = data;
this.next = null;
}
}
//?TIP: When doing a fast.next.next we make sure fast and fast.nexts != null
public class LinkedList {
Node head;
Node tail;
int length;
LinkedList(String data) {
this.head = new Node(data);
this.tail = this.head;
this.length = 1;
}
void append(String data) {
/*
a -> b -> null
*/
Node newNode = new Node(data);
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
void prepend(String data) {
/*
c -> a -> b -> null
*/
Node newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
this.length++;
}
List<String> traverseList() {
List<String> arr = new ArrayList<>();
Node current = this.head;
while(current != null) {
arr.add(current.data);
current = current.next;
}
return arr;
}
// takes the node and the data you want to place after it
void insert(Node node, String data) {
/*
c -> a -> e -> b -> null
*/
Node newNode = new Node(data);
newNode.next = node.next;
node.next = newNode;
this.length++;
}
boolean remove(String data) { //with dummmy node
/*
c -> a -> e -> b -> null
p c
*/
Node current = this.head;
if (current != null && current.data.equals(data)) {
this.head = current.next;
return true;
}
Node prev = null;
while(current != null && !current.data.equals(data)) {
prev = current;
current = current.next;
}
if (current == null) {
return false;
}
prev.next = current.next;
return true;
}
Node removeAllMatchingNodes(String data) {
/*
nul -> e -> c -> b -> f -> c -> null
p
*/
Node dummy = new Node(null);
dummy.next = this.head;
Node prev = dummy;
while (prev.next != null) {
if (prev.next.data.equals(data)) {
prev.next = prev.next.next;
} else {
prev = prev.next;
}
}
this.head = dummy.next;
System.out.println("head -> " + this.head.data);
System.out.println("tail -> " + this.tail.data);
return dummy.next;
}
/*
null <- a <- b -> c -> null
p c
*/
Node reverse() {
Node current = this.head;
Node prev = null;
while (current != null) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
return prev;
}
/*
a->b->c->d->e->null
s
f
*/
String findMiddle() { //fast and slow pointers
Node slow = this.head;
Node fast = this.head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow.data.toString(); // middle element
}
Node deleteNthFromEnd(int n) {
/*
n = 3
null -> a -> b -> c -> d -> e -> f -> null
s
f
*/
Node dummy = new Node(null);
dummy.next = this.head;
Node slow = this.head; Node fast = this.head;
// move fast from 0 -> n times from
for (int i = 0; i < n; i++) {
if (fast.next == null) {
return this.head;
}
fast = fast.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
// slow is before the target so point to the next after the target
slow.next = slow.next.next;
return dummy.next;
}
//TODO
Node removeMiddleNode() {
return this.head;
}
//TODO: swap every two adjacent nodes and return its head
Node swapPairs() {
return this.head;
}
//TODO: reverse between
Node reverseBetween(int m, int n) {
return this.head;
}
//Floyd's algorithm -> Cycle detection in constant time
// we can use a set to keep track of if we have seen a node before (from curr = curr.next) but that is O(n)
boolean hasCycle() {
Node slow = this.head;
Node fast = this.head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
//return the node where the cycle begins
//set can also be used here. When you get to an element already in the set it gets the start of the circle
// and also gives the answer if the list is a cycle.
Node detectCycle() {
Node slow = this.head;
Node fast = this.head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
if (fast == null || fast.next == null) {
return null; //we return null if there is no cycle in the list (if the loop breaks because f or f.next== null)
}
Node current = this.head;
while (slow != current) {
slow = slow.next;
current = current.next;
}
return current; // where the cycles starts
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node current = this.head;
while (current != null) {
sb.append(current.data);
if (current.next != null) sb.append(" -> ");
current = current.next;
}
return sb.toString();
}
}