Data Structures & AlgorithmsHardTCS Digital · Infosys SP

You must check whether a singly linked list of n nodes is a palindrome using O(1) extra space. Which approach works in O(n) time?

  1. A. Copy the values into an array and use two pointers
  2. B. Find the middle with slow/fast pointers, reverse the second half, compare the halves, then (optionally) reverse it back
  3. C. Recursively compare first and last nodes, removing the last each time
  4. D. Push all nodes onto a stack and compare

Correct answer: B. Find the middle with slow/fast pointers, reverse the second half, compare the halves, then (optionally) reverse it back

The stack and the array use O(n) space; repeatedly finding the last node is O(n²). Reversing the second half in place uses O(1) extra space and linear time.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic