Question

Consider the problem of reversing a singly linked list. To take an example, given the linked list below,

Singly linked list starting at head with nodes a, b, c, d, e followed by a ground (null) pointer.

the reversed linked list should look like

Reversed singly linked list starting at head with nodes e, d, c, b, a followed by a ground (null) pointer.

Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O (1) space?

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2022 answer key

Solution

Evaluate the standard iterative three-pointer algorithm for in-place linked list reversal.

A singly linked list with nn nodes can be reversed in-place using three pointers (prev, curr, next):

  1. Initialize prev = NULL and curr = head.
  2. In each iteration, save next = curr->next, reverse the link by setting curr->next = prev, then advance prev = curr and curr = next.
  3. The loop visits each node exactly once, performing O(1)O(1) pointer assignments per node.

This algorithm requires O(1)O(1) auxiliary space and runs in Θ(n)\Theta(n) time in both best and worst cases. Since any algorithm must examine all nn nodes to reverse the list, Ω(n)\Omega(n) time is a lower bound, making Θ(n)\Theta(n) the tight worst-case time complexity. This confirms Option (A) is TRUE.

Final answer

Option (A)

Common trap

Assuming recursive reversal is required, which uses O(n)O(n) call stack space.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2022 Computer Science and Information Technology (CS)
Question
15 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.