Skip to main content

GATE CSE 2022Data Structures › Stacks, Queues, Linked Lists & ArraysVerified

GATE CSE 2022 PYQ: Consider the problem of reversing a singly linked list. To take an example, given the linked list below, the…

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

GATE CSE 2022 Stacks, Queues, Linked Lists & Arrays diagram: Consider the problem of reversing a singly linked list. To t

the reversed linked list should look like

GATE CSE 2022 Stacks, Queues, Linked Lists & Arrays diagram: Consider the problem of reversing a singly linked list. To t

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

  1. A
    The best algorithm for the problem takes θ(n)\theta(n) time in the worst case.
    Correct answer
  2. B
    The best algorithm for the problem takes θ(nlog⁡n)\theta(n \log n) time in the worst case.
  3. C
    The best algorithm for the problem takes θ(n2)\theta(n^{2}) time in the worst case.
  4. D
    It is not possible to reverse a singly linked list in O(1)O(1) space.

Answer

(A) The best algorithm for the problem takes θ ( n ) \theta(n) θ ( n ) time in the worst case.

Approach

The question asks for the worst-case time complexity of reversing a singly linked list when only O(1) auxiliary space is allowed. With constant extra space we may hold only a fixed number of node references, so the only realistic strategy is a single iterative pass that rewires each node's next pointer in place. That immediately gives a linear running time, so the task reduces to confirming the upper bound Θ(n) and arguing the matching Ω(n) lower bound.

Reversing a singly linked list in place needs only a constant number of pointer variables, and each node's next pointer is updated a constant number of times during a single traversal. Every node must be processed at least once, so the worst-case time is exactly Θ(n), making option A the TRUE statement and the other three false.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.