-
Notifications
You must be signed in to change notification settings - Fork 22
/
LinkedListUtil.java
230 lines (200 loc) · 5.74 KB
/
LinkedListUtil.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
package com.company.amazon;
public class LinkedListUtil {
public Node head;
public static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
public void append(int data) {
Node temp = head;
Node newNode = new Node(data);
if (temp == null) {
this.head = newNode;
return;
}
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public static void printList(Node head) {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " , ");
temp = temp.next;
}
System.out.println();
}
public Node reverseListRecursively(Node head) {
if (head == null || head.next == null) {
return head;
}
Node headOfReversedList = reverseListRecursively(head.next);
head.next.next = head;
head.next = null;
return headOfReversedList;
}
public Node reverse(Node head) {
Node temp = head;
Node tempNext = null;
Node prev = null;
while (temp != null) {
tempNext = temp.next;
temp.next = prev;
prev = temp;
temp = tempNext;
}
head = prev;
return head;
}
public Node getMiddleNode(Node head) {
Node slowPointer = head;
Node fastPointer = head;
while (fastPointer != null) {
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
if (fastPointer != null && fastPointer.next == null) {
fastPointer = fastPointer.next;
}
}
return slowPointer;
}
public static boolean isEvenList(Node head) {
int counter = 0;
Node temp = head;
while (temp != null) {
counter++;
temp = temp.next;
}
return counter % 2 == 0 ? true : false;
}
/**
* 1) Find Middle of the List
* 2) Reverse the Second Half
* 3) Compare both 1st half and 2nd Half
* 4) Re-Reverse the Original List
*
* @param head
* @return
*/
public boolean isListPalindrome(Node head) {
Node middleNode = getMiddleNode(head);
Node secondHalfReversedHead = reverse(middleNode);
printList(secondHalfReversedHead);
// This can be calculated as a part of finding the middle node
Boolean isEvenList = isEvenList(head);
Node temp = head;
return compareList(temp, middleNode, secondHalfReversedHead, middleNode, isEvenList);
}
public boolean compareList(Node firstList, Node firstListEnd, Node secondList, Node secondListEnd, Boolean isEvenList) {
Node tempA = firstList;
Node tempB = secondList;
while (tempA != firstListEnd && tempB != null) {
if (tempA.data != tempB.data) {
return false;
}
tempA = tempA.next;
tempB = tempB.next;
if (isEvenList) {
if (tempA == firstListEnd) {
break;
}
}
}
return true;
}
public void swapListInGroup(Node head, int groupCount) {
Node prevHead = null;
Node thisHead = null;
Node prev = null;
Node curr = head;
Node currNext = null;
int count = 0;
while (curr != null) {
thisHead = curr;
count = 0;
prev = null;
while (curr != null && count++ < groupCount) {
currNext = curr.next;
curr.next = prev;
prev = curr;
curr = currNext;
}
if (prevHead == null) {
head = prev;
} else {
prevHead.next = prev;
}
prevHead = thisHead;
}
printList(head);
}
public void removeEveryKNode(Node head, int k) {
int count = 1;
Node prev = null;
while (head != null) {
if (count == k) {
count = 1;
if (prev != null) {
prev.next = head.next;
prev = prev.next;
head = head.next;
} else {
this.head = head.next;
}
} else {
prev = head;
head = head.next;
count++;
}
}
}
public static void main(String[] args) {
LinkedListUtil util = new LinkedListUtil();
util.append(1);
util.append(2);
util.append(3);
util.append(4);
util.append(5);
util.printList(util.head);
util.head = util.reverseListRecursively(util.head);
util.printList(util.head);
// util.append(1);
// util.append(2);
// util.append(3);
// util.append(2);
// util.append(1);
//
// util.printList(util.head);
//
//// util.head = util.reverse(util.head);
//
//// util.printList();
//
// System.out.println("Is List Palindrome ? " + util.isListPalindrome(util.head));
//
// util = new LinkedListUtil();
// util.append(1);
// util.append(2);
// util.append(3);
// util.append(4);
// util.append(5);
// util.append(6);
// util.append(7);
// util.append(8);
// util.append(9);
// util.append(10);
// util.append(11);
//
// util.printList(util.head);
//// util.swapListInGroup(util.head, 3);
//
//
// System.out.println("Removing every kth Node");
// util.removeEveryKNode(util.head, 3);
// util.printList(util.head);
}
}