Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

A singly linked list is 47 -> 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> None. The standard iterative in-place reversal (prev = None; while cur: nxt = cur.next; cur.next = prev; prev = cur; cur = nxt) is run on it. What does the list look like afterwards?

  1. A. 47 -> 29 -> 98 -> 79 -> 78 -> 24 -> 13 -> None
  2. B. 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> 47 -> None
  3. C. 13 -> 29 -> 98 -> 79 -> 78 -> 24 -> 47 -> None
  4. D. 47 -> 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> None

Correct answer: C. 13 -> 29 -> 98 -> 79 -> 78 -> 24 -> 47 -> None

Each node's next pointer is flipped to point to the previous node, reversing the whole list: 13 -> 29 -> 98 -> 79 -> 78 -> 24 -> 47 -> None

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

Exams that ask this topic