Dsa
Linked List
Easy
Detect Cycle in Linked List
Given a linked list, determine if it has a cycle in it. For the sake of this problem, consider an empty linked list as not having a cycle. You must solve this using O(1) space complexity.
Input format:
- The first line contains an integer n, the number of nodes.
- The second line contains n integers, the node values of the list.
- The third line contains an integer m (0 for no cycle, or the node position to form the cycle).
Output format:
- Print 'Yes' if the list has a cycle, otherwise print 'No'.
Example:
Input:
3
1 2 3
2
Output:
Yes
Key concepts
linked_listcycle_detectionpointer_techniques
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now