Dsa
Linked List
Hard
Cycle Detection in Linked List
Given a linked list, determine if it contains a cycle. A cycle occurs when a node points back to a previous node instead of null. Your task is to implement a function that reads the linked list data from standard input and outputs 'Cycle' if there is a cycle or 'No Cycle' if there isn't.
Input Format:
- The first line contains an integer n, the number of nodes in the linked list.
- The second line contains n integers, representing the values of the nodes.
- The third line contains an integer m (0 <= m < n), the index (0-based) of the node that points back to create a cycle; if no cycle, m = -1.
Output Format:
- Print 'Cycle' if a cycle exists, otherwise print 'No Cycle'.
Example:
Input:
5
1 2 3 4 5
2
Output:
Cycle
Key concepts
linked_listcycle_detectionalgorithm
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