Data Structures & AlgorithmsModerate

A singly linked list holds the values 1 2 3 4 5 6 7 in order. It is reordered into L0 → Ln → L1 → Ln−1 → L2 → Ln−2 … using only node re-pointing. Give the resulting value sequence.

  1. A. 1 7 2 6 3 5 4
  2. B. 1 6 2 5 3 4 7
  3. C. 7 1 6 2 5 3 4
  4. D. 7 6 5 4 3 2 1

Correct answer: A. 1 7 2 6 3 5 4

Split the list into a front pointer taking the next node from the start and a back pointer taking the next node from the end, alternating. Starting from the tail or pairing with the wrong neighbour produces a different sequence.

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

Exams that ask this topic