Dsa
Linked List
Warm-up
Detect a Cycle in Linked List
You are given a linked list. Determine if there is a cycle in it. A cycle is present if a node's next points to an earlier node in the list. Your task is to output 'Yes' if a cycle exists and 'No' otherwise.
Input format:
- The first line contains an integer n (0 <= n <= 100), the number of nodes.
- The second line contains n space-separated integers representing the values of the nodes.
- The third line contains two integers u and v (1 <= u, v <= n), indicating that the node u points to node v, thus forming a cycle (0, 0) if no cycle exists.
Output format:
- 'Yes' if a cycle exists, otherwise 'No'.
Example:
Input:
3
1 2 3
1 2
Output:
Yes
Key concepts
linked_listcycle_detection
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