Fast & Slow Pointers · Easy

Linked List Cycle

O(N) · O(1)

Initialize Pointers prev=null, curr=head

Reversing a singly linked list in-place by flipping every next pointer. Set prev = null, curr = node 0 (3).

Linked List Pointers
curr
3
2
0
-4
null
Live Variables & Invariants
prev:null
curr:Node(3)
next:null
Step 1 / 13
8%
Solution Code
1
function hasCycle(head: ListNode | null): boolean {
2
  let slow = head, fast = head;
3
  while (fast && fast.next) {
4
    slow = slow.next!; fast = fast.next.next;
5
    if (slow === fast) return true;
6
  }
7
  return false;
8
}
LeetCode IDE Console

Test Cases

3 cases from Blind 75 & NeetCode 150

Ask for a hint whenever you get stuck.