Dsa
Linked List
Warm-up
Detect Cycle in a Linked List
Write a function to determine if a linked list has a cycle. You should return True if there is a cycle, otherwise return False.
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 indicates the index of a node that points back to one of the previous nodes (or -1 if no cycle).
Output Format:
- Print True if there's a cycle, False otherwise.
Example:
Input:
4
1 2 3 4
1
Output:
True
Key concepts
linked_listcycletraversal
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