-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.java
229 lines (188 loc) · 5.49 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
/*
* https://www.techiedelight.com/clone-given-linked-list/
* https://www.techiedelight.com/split-nodes-given-linked-list-front-back-halves/
*/
import java.util.Random;
class LinkedList {
private class Node {
int data;
Node next;
public Node(int d) {
data = d;
next = null;
}
}
private Node head;
public LinkedList() {
head = null;
}
public boolean isEmpty() {
return head == null;
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println("");
}
public void addFirst(int data) {
Node temp = new Node(data);
if (head == null) {
head = temp;
} else {
temp.next = head;
head = temp;
}
}
public void addLast(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Node(data);
}
public int pop() {
if (head == null) {
System.out.println("Linked List underflow");
return Integer.MIN_VALUE;
}
Node temp = head;
head = head.next;
temp.next = null;
return temp.data;
}
public int deleteLast() {
if (head == null) {
System.out.println("Linked List underflow");
return Integer.MIN_VALUE;
}
Node temp = head;
Node prev = null;
while (temp.next != null) {
prev = temp;
temp = temp.next;
}
if (prev == null) {
head = null;
} else {
prev.next = null;
}
return temp.data;
}
public LinkedList clone() {
if (isEmpty()) {
return new LinkedList();
}
LinkedList cloned = new LinkedList();
Node ptr = head;
Node tail = null;
while (ptr != null) {
if (tail == null) {
cloned.head = new Node(ptr.data);
tail = cloned.head;
} else {
tail.next = new Node(ptr.data);
tail = tail.next;
}
ptr = ptr.next;
}
return cloned;
}
private Node cloneRec(Node node) {
if (node == null) {
return null;
}
Node temp = new Node(node.data);
temp.next = cloneRec(node.next);
return temp;
}
public LinkedList cloneRec() {
if (isEmpty()) {
return new LinkedList();
}
LinkedList cloned = new LinkedList();
cloned.head = cloneRec(head);
return cloned;
}
public LinkedList[] splitList() {
LinkedList front = new LinkedList();
LinkedList back = new LinkedList();
if (head == null || head.next == null) {
front.head = head;
back.head = null;
return new LinkedList[] {front, back};
}
Node slow = head;
Node fast = head.next;
while (fast != null) {
fast = fast.next;
if (fast != null) {
fast = fast.next;
slow = slow.next;
}
}
back.head = slow.next;
front.head = head;
slow.next = null;
return new LinkedList[] {front, back};
}
}
class Driver {
private static Random random = new Random();
private static void printArray(int[] array) {
for (int a: array) {
System.out.print(a + " ");
}
System.out.println("");
}
private static int getNumber(int max) {
return random.nextInt(max);
}
private static int[] getRandomArray(int max, int numbers) {
int[] array = new int[numbers];
for (int i = 0; i < numbers; i++) {
array[i] = random.nextInt(max) + 1;
}
return array;
}
public static void main(String[] args) {
int max = 15;
int num = 20;
int[] array = Driver.getRandomArray(max, num);
Driver.printArray(array);
System.out.println("======================================================");
LinkedList list = new LinkedList();
for (int a : array) {
list.addFirst(a);
}
list.printList();
System.out.println("======================================================");
LinkedList list2 = new LinkedList();
for (int a : array) {
list2.addLast(a);
}
list2.printList();
System.out.println("list2 pop : " + list2.pop());
System.out.println("list2 delete last node : " + list2.deleteLast());
list2.printList();
System.out.println("======================================================");
System.out.println("Cloning list2");
LinkedList cloned = list2.clone();
cloned.printList();
System.out.println("======================================================");
System.out.println("Cloning list2 recursively");
LinkedList clonedR = list2.clone();
clonedR.printList();
System.out.println("======================================================");
System.out.println("Splitting list1 int 2 halves");
LinkedList[] splits = list.splitList();
splits[0].printList();
splits[1].printList();
}
}