
Problem
Solution
Submissions
Linked List has a Cycle
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C# program to determine if a linked list has a cycle in it. A cycle occurs when a node in the linked list points to a previously visited node, forming a loop.
Example 1
- Input: head = [3, 2, 0, -4], pos = 1
- Output: true
- Explanation:
- There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2
- Input: head = [1, 2], pos = 0
- Output: true
- Explanation:
- There is a cycle in the linked list, where the tail connects to the 0th node.
Constraints
- The number of nodes in the list is in the range [0, 10^4]
- -10^5 ≤ Node.val ≤ 10^5
- pos is -1 or a valid index in the linked-list
- You must solve this problem without modifying the linked list structure
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use Floyd's Cycle-Finding Algorithm (also known as the "tortoise and hare" approach)
- Use two pointers moving at different speeds
- If there is a cycle, the fast pointer will eventually meet the slow pointer
- If there is no cycle, the fast pointer will reach the end of the list