Data Structures & AlgorithmsVery hard
Floyd's cycle detection finds the slow and fast pointers meeting inside the loop. How is the START of the loop then found?
- A. Reverse the list and run the pointers again from the tail.
- B. Restart the slow pointer at the head and advance both pointers one step at a time; they meet at the loop start.
- C. Count the nodes until the first repeated value appears.
- D. Advance only the fast pointer until it returns to the meeting point.
Correct answer: B. Restart the slow pointer at the head and advance both pointers one step at a time; they meet at the loop start.
The distance from the head to the loop start equals the distance from the meeting point to the loop start (modulo the loop length). Moving one pointer back to the head and stepping both at speed one makes them meet exactly at the start.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In the greedy patching algorithm, the reachable range is [1..R] and the next sorted value x exceeds R+1. Why is R+1 the right patch?
- A monotonic deque for sliding-window maximum stores array INDICES instead of values. Why?
- When tracking a '' wildcard that may be '(', ')' or empty while scanning brackets, why is it enough to keep a range [lo, hi] of possible…
- Two different strings produced equal rolling-hash values. What does this tell you?
- In iterative reversal of a singly linked list, which pointer must be saved BEFORE the current node's next pointer is changed?
- For sorted positive integers, the smallest value that cannot be formed as a subset sum equals R+1, where R is the reach. Why does a value…