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.
- A. 1 7 2 6 3 5 4
- B. 1 6 2 5 3 4 7
- C. 7 1 6 2 5 3 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.
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
- From the linked list 5 2 13 3 8 7 9 4, delete every node that has a strictly greater value somewhere AFTER it in the list. Which list…
- A min-stack supports push, pop and getMin in O(1). Apply this sequence, starting empty: push 5, push 3, push 7, getMin, pop, push 1…
- Asteroids in a row have sizes given by 5 8 -3 -8 10 -4 2. Positive values move right, negative values move left; two meeting asteroids…
- People stand in a row with heights [10, 6, 8, 5, 11, 9]. Person i can see person j > i if every person strictly between them is shorter…
- In the unsorted array 100 4 200 1 3 2 4 101 102 103 3, what is the length of the longest run of CONSECUTIVE integers (values like 5, 6, 7…
- For the array 1 2 3 4, how many DISTINCT values appear as the XOR of a non-empty contiguous subarray?