-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
232 lines (194 loc) · 5 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
232 lines (194 loc) · 5 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
import java.util.HashSet;
/*A singly linked list which holds integer data .
*
* Date 21/4/2018
*
* @author:Debapriya Biswas
*
*
**/
public class SinglyLinkedList {
SinglyLinkedListNode head;
SinglyLinkedListNode tail;
class SinglyLinkedListNode {
int data;
SinglyLinkedListNode next;
public SinglyLinkedListNode(int data) {
this.data = data;
this.next = null;
}
}
SinglyLinkedList() {
this.head = this.tail = null;
}
/*
* Inserts a node at the end of the list. If the list a empty this node
* becomes the head
*
*/
public void insertNode(int data) {
SinglyLinkedListNode node = new SinglyLinkedListNode(data);
if (head == null)
head = node;
else
tail.next = node;
tail = node;
}
/*
* Reverses a linked list . the size and the number of nodes in the list
* remains unchanged
*/
public SinglyLinkedListNode reverse() {
SinglyLinkedListNode prev = null;
SinglyLinkedListNode current = head;
SinglyLinkedListNode next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
/*
* Sorts a Linked List into batches of k Returns head of the result list
*
*/
public SinglyLinkedListNode kSort(int k) {
return kSort(k, head);
}
private SinglyLinkedListNode kSort(int k, SinglyLinkedListNode list) {
SinglyLinkedListNode pointer = list;
SinglyLinkedListNode nextPointer = list;
SinglyLinkedList result = new SinglyLinkedList();
while (nextPointer != null) {
pointer = list;
for (int i = 1; i < k; i++) {
pointer = pointer.next;
}
if (pointer == null)
nextPointer = null;
else {
nextPointer = pointer.next;
pointer.next = null;
}
if (result.head == null)
result.head = sort(list);
else {
SinglyLinkedListNode track = result.head;
while (track.next != null)
track = track.next;
track.next = sort(list);
}
list = nextPointer;
}
return result.head;
}
/* Sorts a linked list */
public SinglyLinkedListNode sort(SinglyLinkedListNode list) {
return mergeSort(list);
}
private SinglyLinkedListNode mergeSort(SinglyLinkedListNode list) {
if (list == null || list.next == null)
return list;
SinglyLinkedListNode middle = getMiddle(list);
SinglyLinkedListNode midNext = middle.next;
middle.next = null;
SinglyLinkedListNode left = mergeSort(list);
SinglyLinkedListNode right = mergeSort(midNext);
SinglyLinkedListNode result = merge(left, right);
return result;
}
/*
* Gets the middle of the linked list if the number of nodes in the linked
* list is even then the first out of the two possibilities is returned , In
* case of odd middle element is trivial
*
*/
public SinglyLinkedListNode getMiddle(SinglyLinkedListNode list) {
if (list == null)
return null;
SinglyLinkedListNode fastPtr = list.next;
SinglyLinkedListNode slowPtr = list;
while (fastPtr != null) {
fastPtr = fastPtr.next;
if (fastPtr != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next;
}
}
return slowPtr;
}
private SinglyLinkedListNode merge(SinglyLinkedListNode a, SinglyLinkedListNode b) {
SinglyLinkedListNode result = null;
if (a == null)
return b;
if (b == null)
return a;
if (a.data <= b.data) {
result = a;
result.next = merge(a.next, b);
} else {
result = b;
result.next = merge(a, b.next);
}
return result;
}
/* Removes Duplicate elements in the linked list */
public void removeDuplicates() {
this.head = sort(this.head);
SinglyLinkedListNode e = head;
while (e.next != null) {
if (e.data == e.next.data)
e.next = e.next.next;
else
e = e.next;
}
}
private void addOneUtil() {
SinglyLinkedListNode next = head;
SinglyLinkedListNode temp = null;
int carry = 1;
int sum = 0;
while (next != null) {
sum = carry + next.data;
carry = sum >= 10 ? 1 : 0;
sum = sum % 10;
next.data = sum;
temp = next;
next = next.next;
}
if (carry >= 1)
temp.next = new SinglyLinkedListNode(carry);
}
/* Adds 1 to a number where each digits is a node is a linked list */
public void addOne(SinglyLinkedListNode node) {
head = reverse();
addOneUtil();
head = reverse();
}
public String toString() {
SinglyLinkedListNode pointer = this.head;
StringBuilder sb = new StringBuilder();
while (pointer != null) {
sb.append(pointer.data).append("->");
pointer = pointer.next;
}
return sb.toString();
}
/* Detects loop in a linked list */
public boolean detectLoop(SinglyLinkedListNode h) {
/*
* works only if equals() and hashCode() in SinglyLinkedListNode is not
* overidden
*/
HashSet<SinglyLinkedListNode> s = new HashSet<SinglyLinkedListNode>();
while (h != null) {
if (s.contains(h))
return true;
s.add(h);
h = h.next;
}
return false;
}
}