-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListAp.java
More file actions
212 lines (195 loc) · 5.37 KB
/
LinkedListAp.java
File metadata and controls
212 lines (195 loc) · 5.37 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
import java.util.HashSet;
public class LinkedListAp {
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static Node head = null;
public static Node tail = null;
public static int size = 0;
public Node deleteN_nodes_after_Mnodes(int N, int M) {
Node temp = head;
Node erase;
while (temp != null) {
for (int i = 0; i < M - 1; i++) {
temp = temp.next;
}
erase = temp.next;
for (int i = 0; i < N; i++) {
if (erase != null) {
erase = erase.next;
}
}
temp.next = erase;
temp = temp.next;
}
return head;
}
public void addLast(int data) {
Node node = new Node(data);
size++;
if (head == null) {
head = tail = node;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
tail = node;
}
public void printLL() {
if (head == null) {
System.out.println("null");
return;
}
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.println("null");
}
public void reversell() {
if (head == null) {
System.out.println("Empty LL");
return;
}
Node prev = null;
Node cur = tail = head;
Node next;
while (cur != null) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
head = prev;
}
public void connectLastNthtoFirst(int n) {
int i = 1;
Node temp = head;
while (i < n && temp != null) {
temp = temp.next;
i++;
}
Node lastTemp = head;
while (lastTemp.next != null) {
lastTemp = lastTemp.next;
}
tail = lastTemp;
tail.next = temp;
}
public void printCycle() {
if (head == null) {
System.out.println("Empty ll");
return;
}
HashSet<Integer> hs = new HashSet<>();
Node temp = head;
while (temp != null) {
if (!hs.contains(temp.data)) {
hs.add(temp.data);
System.out.print(temp.data + "->");
temp = temp.next;
} else {
System.out.print(temp.data + " cycle starts\n");
return;
}
}
System.out.println();
}
public void findCycle() {
Node slow = head, fast = head;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;// Here the slow is not neccesary for the looping becasue the slow will always
// be behind the fast node
fast = fast.next.next;
if (slow == fast) {
System.out.println("cycle present");
return;
}
}
System.out.println("cycle NOT present");
}
// Using the slow-fast approach we will find the midNode
public Node midNode() {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public void checkPalindrome() {
Node mid = midNode();
Node prev = null;
Node cur = mid;
Node next;
while (cur != null) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
Node last = prev;
Node temp = head;
while (last != null && temp != null) {
if (last.data != temp.data) {
System.out.println("not palindrome");
return;
}
last = last.next;
temp = temp.next;
}
System.out.println("isPalindrome");
}
public void removeCycle() {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
slow = head;
Node prev = fast;
while (fast != slow) {
prev = fast;
fast = fast.next;
slow = slow.next;
}
prev.next = null;
// copilote suggestion for how to handle the possible null value of the fast
// node
// while (fast != null && fast.next != null && fast != slow) {
// prev = fast;
// fast = fast.next;
// slow = slow.next;
// }
// if (fast != null && prev != null) {
// prev.next = null;
// }
}
public static void main(String[] args) {
LinkedListAp ll = new LinkedListAp();
ll.addLast(1);
ll.addLast(2);
ll.addLast(3);
ll.addLast(4);
ll.addLast(5);
ll.connectLastNthtoFirst(2);
ll.printCycle();
ll.findCycle();
// ll.checkPalindrome();
ll.removeCycle();
ll.printCycle();
ll.findCycle();
}
}