Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
A singly linked list is 95 -> 83 -> 36 -> 42 -> 99 -> 64 -> 72 -> None. The first 5 nodes are reversed in place (the node that was originally the head becomes the tail of that reversed portion, and is then linked to the node that originally followed the 5th node). What is the resulting list?
- A. 72 -> 64 -> 99 -> 42 -> 36 -> 83 -> 95 -> None
- B. 99 -> 42 -> 36 -> 83 -> 95 -> 72 -> None
- C. 99 -> 42 -> 36 -> 83 -> 95 -> 64 -> 72 -> None
- D. 95 -> 83 -> 36 -> 42 -> 99 -> 64 -> 72 -> None
Correct answer: C. 99 -> 42 -> 36 -> 83 -> 95 -> 64 -> 72 -> None
Reversing only the first 5 nodes and re-linking the old head to what followed the 5th node gives: 99 -> 42 -> 36 -> 83 -> 95 -> 64 -> 72 -> None
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
- Using a monotonic (increasing) stack to find, for each element of [20, 17, 18, 14, 44, 28], the nearest element to its left that is…
- A monotonic decreasing stack of indices is used to compute the next-greater-element array for [48, 12, 29, 6, 30, 28] (push index i after…
- A singly linked list is 47 -> 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> None. The standard iterative in-place reversal (prev = None; while cur…
- Using the sliding window technique, what is the maximum sum of any 8 consecutive elements in [24, 30, 4, 27, 25, 2, 13, 22, 26, 1]?
- A singly linked list has nodes at positions 0..6 holding 58 -> 20 -> 49 -> 17 -> 78 -> 37 -> 22, but the last node's next pointer points…
- Heap sort (build a max-heap, then repeatedly swap the root with the last unsorted element and sift down) is run on [1, 27, 40, 40, 63, 2…