The Linked List program is available here: link
public boolean isLoopExist() {
LinkedList L = this;
LinkedList slow = L;
LinkedList fast = L;
boolean isLoop = false;
while(fast.next!=null) {
slow=slow.next;
fast=fast.next.next;
if(slow==fast) {
isLoop = true;
break;
}
}
return isLoop;
}
public void createLoop(int index) {
LinkedList L = this;
for(int i=2; i<index; i++) {
L=L.next;
}
LinkedList indexPrevNode = L;
while(L.next!=null) {
L=L.next;
}
L.next=indexPrevNode.next;
}
No comments:
Post a Comment