Check a Linked List for a Cycle
Updated
Problem statement
You are given head, the first node of a singly linked list. Every node stores an integer val and a pointer next to the node after it. Start at head and keep moving to next. If this walk ever arrives at a node it has seen before, it can never stop, and we say there is a cycle. Return true if there is a cycle. Return false if the walk reaches a node whose next is null, or if the list is empty.
Each test writes a list as [values, pos]. values gives the node values in order, starting at head. pos tells where the last node's next leads: the index of that node in values, or -1 when that next is null. Indexes start at 0. The tests use pos to build the links; your function gets only head.
Function
hasCycle(head: CycleListNode) → booleanExamples
Example 1
head = [[4,9,-2,7,5],1]return = trueThe last node, 5 at index 4, links back to 9 at index 1. Starting at head, the walk goes 4, 9, -2, 7, 5 and then arrives at 9 again, so it never stops.
Example 2
head = [[6,1,8],-1]return = falseHere pos is -1, so the last node 8 has next set to null. The walk visits three nodes and stops.
Example 3
head = [[3],0]return = trueThe single node's next leads back to it, so each step lands on it again.
Example 4
head = [[5,5],-1]return = falseBoth nodes hold 5, but they are two different nodes, and the second one's next is null. A repeated value is not a cycle; the walk must return to a node it has passed.
Constraints
0 <= values.length <= 104-105 <= values[i] <= 105posis-1or an index ofvalues, that is0 <= pos < values.length
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
- Merge Point of Two Linked ListsBank of America · OABank of America interview questions
- Find Where Two Linked Lists MergeExpedia · ONSITE INTERVIEWExpedia interview questions
- Detect a Linked-List CyclePaytm · ONSITE INTERVIEWPaytm interview questions
- Detect a Linked-List CycleCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Detect a Linked-List CycleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Middle of a Linked ListCisco · PHONE SCREENCisco interview questions