Skip to content

Commit b2aba3e

Browse files
committed
linked list cycle done
1 parent 4b89f45 commit b2aba3e

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.leetcode.linkedlists;
2+
3+
/**
4+
* Level: Medium
5+
* Link: https://leetcode.com/problems/linked-list-cycle-ii/
6+
* Description:
7+
* Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
8+
*
9+
* To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in
10+
* the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
11+
*
12+
* Note: Do not modify the linked list.
13+
*
14+
* Example 1:
15+
*
16+
* Input: head = [3,2,0,-4], pos = 1
17+
* Output: tail connects to node index 1
18+
* Explanation: There is a cycle in the linked list, where tail connects to the second node.
19+
*
20+
*
21+
* Example 2:
22+
*
23+
* Input: head = [1,2], pos = 0
24+
* Output: tail connects to node index 0
25+
* Explanation: There is a cycle in the linked list, where tail connects to the first node.
26+
*
27+
*
28+
* Example 3:
29+
*
30+
* Input: head = [1], pos = -1
31+
* Output: no cycle
32+
* Explanation: There is no cycle in the linked list.
33+
*
34+
* Follow-up:
35+
* Can you solve it without using extra space?
36+
*
37+
* @author rampatra
38+
* @since 2019-08-18
39+
*/
40+
public class LinkedListCycleII {
41+
42+
/**
43+
* Runtime: <a href="https://leetcode.com/submissions/detail/252786957/">0 ms</a>.
44+
*
45+
* @param head
46+
* @return
47+
*/
48+
public Node detectCycle(Node head) {
49+
Node slow = head;
50+
Node fast = head;
51+
52+
while (fast != null && fast.next != null) {
53+
slow = slow.next;
54+
fast = fast.next.next;
55+
if (slow == fast) {
56+
break;
57+
}
58+
}
59+
60+
if (fast == null || fast.next == null) {
61+
return null;
62+
} else {
63+
slow = head;
64+
65+
while (slow != fast) {
66+
slow = slow.next;
67+
fast = fast.next;
68+
}
69+
70+
return slow;
71+
}
72+
}
73+
}

src/main/java/com/rampatra/linkedlists/DetectAndRemoveLoop.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public class DetectAndRemoveLoop {
3434
* ii. Now, move both slow and fast pointer at same pace and where they meet is the starting point of the loop.
3535
* iii. Lastly, to remove the loop make the next of the node (before the starting point of loop) to null.
3636
*
37+
* Proof for Floyd's Cycle Detection: Consider a cyclic list and imagine the slow and fast pointers are two runners
38+
* racing around a circle track. The fast runner will eventually meet the slow runner. Why? Consider this case -
39+
* The fast runner is just one step behind the slow runner. In the next iteration, they both increment one and two
40+
* steps respectively and meet each other.
41+
*
3742
* @param list
3843
* @param <E>
3944
* @return {@code true} if loop exists {@code false} otherwise.
@@ -58,7 +63,7 @@ public static <E extends Comparable<E>> boolean detectAndRemoveLoop(SingleLinked
5863
while (true) {
5964
slow = slow.next;
6065
fast = fast.next;
61-
if (slow.next == fast.next) {
66+
if (slow == fast) {
6267
fast.next = null;
6368
break;
6469
}
@@ -78,5 +83,23 @@ public static void main(String[] args) {
7883
linkedList.getNode(4).next = linkedList.getNode(2);
7984
System.out.println(detectAndRemoveLoop(linkedList));
8085
linkedList.printList();
86+
87+
linkedList = new SingleLinkedList<>();
88+
linkedList.add(0);
89+
linkedList.add(1);
90+
linkedList.getNode(1).next = linkedList.getNode(0);
91+
System.out.println(detectAndRemoveLoop(linkedList));
92+
linkedList.printList();
93+
94+
linkedList = new SingleLinkedList<>();
95+
linkedList.add(0);
96+
System.out.println(detectAndRemoveLoop(linkedList));
97+
linkedList.printList();
98+
99+
linkedList = new SingleLinkedList<>();
100+
linkedList.add(0);
101+
linkedList.getNode(0).next = linkedList.getNode(0);
102+
System.out.println(detectAndRemoveLoop(linkedList));
103+
linkedList.printList();
81104
}
82105
}

0 commit comments

Comments
 (0)