Skip to content

Commit ab18990

Browse files
committed
added Linked List Cycle II
1 parent 5d58eed commit ab18990

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

Medium/LinkedListCycle2/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# Linked List Cycle II
3+
[Leetcode Link](https://leetcode.com/problems/linked-list-cycle-ii/)
4+
5+
## Problem:
6+
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 the linked list where tail connects to. If `pos` is `-1`, then there is no cycle in the linked list.
10+
11+
Note: Do not modify the linked list.
12+
13+
## Example:
14+
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+
![example](assets/example1.png)
21+
```
22+
Input: head = [1,2], pos = 0
23+
Output: tail connects to node index 0
24+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
25+
```
26+
![example](assets/example2.png)
27+
```
28+
Input: head = [1], pos = -1
29+
Output: no cycle
30+
Explanation: There is no cycle in the linked list.
31+
```
32+
![example](assets/example3.png)
33+
34+
## Note:
35+
36+
Can you solve it without using extra space?
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package Medium.LinkedListCycle2;
2+
3+
// Definition for singly-linked list.
4+
class ListNode {
5+
int val;
6+
ListNode next;
7+
ListNode(int x) {
8+
val = x;
9+
next = null;
10+
}
11+
}
12+
13+
/**
14+
* Solution
15+
* Use the hare and tortoise algorithm to detect cycle
16+
*/
17+
public class Solution {
18+
19+
public ListNode detectCycle(ListNode head) {
20+
// empty list
21+
if (head == null) {
22+
return null;
23+
}
24+
ListNode hare = head;
25+
ListNode tortoise = head;
26+
do {
27+
tortoise = tortoise.next;
28+
// hare moves twice as fast as tortoise
29+
hare = hare.next;
30+
// if gets to the end of list (null), then no cycle
31+
if (hare == null) {
32+
return null;
33+
}
34+
hare = hare.next;
35+
if (hare == null) {
36+
return null;
37+
}
38+
} while (hare != tortoise);
39+
// System.out.println("First met at (value): " + hare.val);
40+
// return the tortoise to the beginning and move both hare and tortoise one step at a time
41+
tortoise = head;
42+
while (hare != tortoise) {
43+
tortoise = tortoise.next;
44+
hare = hare.next;
45+
}
46+
// System.out.println("Cycle starts at (value): " + tortoise.val);
47+
// the second time they meet is the entry point of the cycle (return either tortoise or hare)
48+
return tortoise;
49+
}
50+
51+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package Medium.LinkedListCycle2;
2+
3+
import java.util.Arrays;
4+
5+
public class TestDriver {
6+
7+
public static void main(String[] args) {
8+
Solution sol = new Solution();
9+
int i = 0;
10+
11+
int[] arr1 = {3,2,0,-4};
12+
int pos1 = 1;
13+
ListNode head1 = createLinkedList(arr1, pos1);
14+
System.out.println("Input: head = " + Arrays.toString(arr1) + ", pos = " + pos1);
15+
ListNode cycleEntry1 = sol.detectCycle(head1);
16+
if (cycleEntry1 == null) {
17+
System.out.println("Output: no cycle");
18+
} else {
19+
while (head1 != cycleEntry1) {
20+
i++;
21+
head1 = head1.next;
22+
}
23+
System.out.println("Output: tail connects to node index " + i);
24+
}
25+
System.out.println();
26+
27+
i = 0;
28+
int[] arr2 = {1,2};
29+
int pos2 = 0;
30+
ListNode head2 = createLinkedList(arr2, pos2);
31+
System.out.println("Input: head = " + Arrays.toString(arr2) + ", pos = " + pos2);
32+
ListNode cycleEntry2 = sol.detectCycle(head2);
33+
if (cycleEntry2 == null) {
34+
System.out.println("Output: no cycle");
35+
} else {
36+
while (head2 != cycleEntry2) {
37+
i++;
38+
head2 = head2.next;
39+
}
40+
System.out.println("Output: tail connects to node index " + i);
41+
}
42+
System.out.println();
43+
44+
int[] arr3 = {1};
45+
int pos3 = -1;
46+
ListNode head3 = createLinkedList(arr3, pos3);
47+
System.out.println("Input: head = " + Arrays.toString(arr3) + ", pos = " + pos3);
48+
ListNode cycleEntry3 = sol.detectCycle(head3);
49+
if (cycleEntry3 == null) {
50+
System.out.println("Output: no cycle");
51+
} else {
52+
while (head3 != cycleEntry3) {
53+
i++;
54+
head3 = head3.next;
55+
}
56+
System.out.println("Output: tail connects to node index " + i);
57+
}
58+
System.out.println();
59+
}
60+
61+
// helper to construct list from input array and pos
62+
// returns the head node
63+
public static ListNode createLinkedList(int[] arr, int pos) {
64+
ListNode head = new ListNode(arr[0]);
65+
ListNode temp = head;
66+
for (int i = 1; i < arr.length; i++) {
67+
ListNode newNode = new ListNode(arr[i]);
68+
temp.next = newNode;
69+
temp = newNode;
70+
}
71+
if (pos >= 0) {
72+
// find the node where cycle begins
73+
ListNode iter = head;
74+
for (int i = 0; i < pos; i++) {
75+
iter = iter.next;
76+
}
77+
// temp is pointing to the last node in the list
78+
temp.next = iter;
79+
}
80+
return head;
81+
}
82+
}
11 KB
Loading
4.47 KB
Loading
1.92 KB
Loading

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Languages used: Java and Python
5050
- [Prison Cells After N Days](Medium/PrisonCellsAfterNDays)
5151
- [Minimum Number of Steps to Make Two Strings Anagram](Medium/MinStepsToMakeTwoStringsAnagram)
5252
- [Maximum Length of Pair Chain](Medium/MaximumLengthPairChain)
53+
- [Linked List Cycle II](Medium/LinkedListCycle2)
5354
- Hard
5455

5556
---

0 commit comments

Comments
 (0)