GATE 2022 CS – Question 15
Consider the problem of reversing a singly linked list. To take an example, given the linked list a → b → c → d → e, the reversed linked list should look like e → d → c → b → a.
Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in $O(1)$ space?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (A) The best algorithm for the problem takes $\theta(n)$ time in the worst case.
Explanation
Iterative pointer reversal with three pointers (prev, curr, next) takes $\theta(n)$ time and $O(1)$ space. Every node must be visited, so $\Omega(n)$ is also needed.