0% completed
Problem Statement
Given the head of a Singly LinkedList that contains a cycle, write a function to find the starting node of the cycle.

Solution
If we know the length of the LinkedList cycle, we can find the start of the cycle through the following steps:
- Take two pointers. Let’s call them
pointer1
andpointer2
. - Initialize both pointers to point to the start of the LinkedList.
- We can find the length of the LinkedList cycle using the approach discussed in LinkedList Cycle. Let’s assume that the length of the cycle is ‘K’ nodes.
- Move
pointer2
ahead by ‘K’ nodes. - Now, keep incrementing
pointer1
andpointer2
until they both meet. - As
pointer2
is ‘K’ nodes ahead ofpointer1
, which means,pointer2
must have completed one loop in the cycle when both pointers meet. Their meeting point will be the start of the cycle.
Let’s visually see this with the above-mentioned Example-1:

We can use the algorithm discussed in LinkedList Cycle to find the length of the cycle and then follow the above-mentioned steps to find the start of the cycle.
Code
Here is what our algorithm will look like:
Complexity Analysis
Time Complexity
-
Phase 1: Cycle detection:
- The algorithm uses Floyd's Tortoise and Hare (fast and slow pointer) approach to detect a cycle in the linked list. Both pointers traverse the list at different speeds, with the fast pointer moving twice as fast as the slow pointer. In the worst case, they traverse the entire list once until they meet inside the cycle or confirm there is no cycle.
- This phase takes O(N) time, where
N
is the number of nodes in the linked list.
-
Phase 2: Cycle length calculation:
- After detecting the cycle, the algorithm calculates the cycle's length by traversing the cycle with the
slow
pointer. The maximum number of steps taken is equal to the length of the cycle, denoted as C. Since C \leq N, this phase is bounded by O(N) time.
- After detecting the cycle, the algorithm calculates the cycle's length by traversing the cycle with the
-
Phase 3: Finding the start of the cycle:
- Once the cycle length is known, the algorithm uses two pointers to find the start of the cycle. The first pointer starts from the head, while the second pointer is moved ahead by
C
nodes. Both pointers then traverse the list one step at a time until they meet at the cycle's start. Since each pointer traverses part of the list, this phase also takes O(N) time.
- Once the cycle length is known, the algorithm uses two pointers to find the start of the cycle. The first pointer starts from the head, while the second pointer is moved ahead by
Overall time complexity: O(N), where N
is the total number of nodes in the linked list.
Space Complexity
- The algorithm uses a few extra variables, including two pointers (
slow
andfast
) and some helper variables for calculating the cycle length and finding the start of the cycle. These variables require constant space. - No additional data structures that depend on the input size are used.
Overall space complexity: O(1) (constant space).
An Alternate Approach
Here is the second approach to solve the "Start of LinkedList Cycle" problem using Floyd’s Tortoise and Hare algorithm. This method employs two pointers moving at different speeds to detect the cycle and identify its starting node.
The algorithm begins by initializing two pointers, slow
and fast
, both starting at the head of the linked list. The slow
pointer moves one step at a time, while the fast
pointer moves two steps. If there is a cycle, these pointers will eventually meet inside the cycle. Once a meeting point is found, the slow
pointer is reset to the head, and both pointers move one step at a time. The point where they meet again is the start of the cycle.
Step-by-Step Algorithm
Phase 1: Detecting the Cycle Using Two Pointers
-
Initialize Pointers:
- Set both
slow
andfast
pointers to the head of the linked list to start traversing from the beginning.
- Set both
-
Traverse to Detect Cycle:
- Loop while
fast
andfast.next
are notnull
:- Move
slow
one step forward. - Move
fast
two steps forward. - If
slow
andfast
meet, a cycle is detected and exit the loop.
- Move
- Loop while
-
Check for No Cycle:
- After the loop, if
fast
isnull
orfast.next
isnull
, returnnull
as there is no cycle in the list.
- After the loop, if
Phase 2: Finding the Start of the Cycle
-
Reset
slow
Pointer:- Move
slow
back to the head of the linked list to begin locating the cycle's start.
- Move
-
Move Both Pointers to Find Cycle Start:
- Loop until
slow
equalsfast
:- Move
slow
one step forward. - Move
fast
one step forward.
- Move
- When they meet,
slow
(orfast
) points to the cycle's starting node.
- Loop until
-
Return the Starting Node:
- Return the node where
slow
andfast
meet as the start of the cycle.
- Return the node where
Algorithm Walkthrough
Input: List = [1, 2, 3, 4, 5, 6]
, Cycle Start at Node 3
Linked List Structure:
1 -> 2 -> 3 -> 4 -> 5 -> 6
^ |
|--------------|
Steps:
-
Initialize Pointers:
slow = 1
,fast = 1
-
First Iteration:
- Move
slow
to2
. - Move
fast
to3
. slow != fast
- Move
-
Second Iteration:
- Move
slow
to3
. - Move
fast
to5
. slow != fast
- Move
-
Third Iteration:
- Move
slow
to4
. - Move
fast
to3
(sincefast
was at5
,fast.next
is6
, andfast.next.next
is3
). slow != fast
- Move
-
Fourth Iteration:
- Move
slow
to5
. - Move
fast
to5
. slow == fast
(Cycle detected)
- Move
-
Find Cycle Start:
- Reset
slow
to1
. - Move both
slow
andfast
one step at a time:- Move
slow
to2
,fast
to6
. - Move
slow
to3
,fast
to3
.
- Move
slow == fast
at node3
(Cycle starts here)
- Reset
-
Return Cycle Start:
- The start of the cycle is node
3
.
- The start of the cycle is node
Code
Complexity Analysis
Time Complexity
-
Cycle Detection:
- The initial loop where
slow
andfast
pointers traverse the linked list runs in O(n) time in the worst case, wheren
is the number of nodes in the list. - The
slow
pointer moves one step at a time, and thefast
pointer moves two steps. They will meet within a finite number of steps proportional to the number of nodes.
- The initial loop where
-
Finding Cycle Start:
- After detecting the cycle, resetting
slow
tohead
and moving bothslow
andfast
one step at a time also takes O(n) time in the worst case.
- After detecting the cycle, resetting
-
Overall Time Complexity:
- The combined operations result in a total time complexity of O(n).
Space Complexity
-
Pointer Variables:
- Only a constant number of pointers (
slow
andfast
) are used, resulting in O(1) space.
- Only a constant number of pointers (
-
No Additional Data Structures:
- The algorithm does not utilize any additional data structures that scale with input size.
-
Overall Space Complexity:
- The space complexity is O(1).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible