Dsa
Linked List
Easy
Detect Loop in Linked List
Given a singly linked list, determine if it has a cycle in it. An algorithm should return 'YES' if there is a cycle and 'NO' otherwise.
Input Format:
- First line: An integer n (the number of nodes).
- Second line: n space-separated integers representing the values of the nodes.
- The third line: Two integers a, b (0-based indices) indicating a connection to create a cycle. If a == -1, then there is no cycle.
Output Format:
- A single line containing 'YES' or 'NO'.
Example:
Input:
5
1 2 3 4 5
1 2
Output:
YES
Key concepts
linked_listloop_detectionfast_slow
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