Exams › GATE › Technical
Consider the problem of reversing a singly linked list. To take an example, given the linked list below, head -> a -> b -> c -> d -> e -> NULL, the reversed linked list should look like head -> e -> d -> c -> b -> a -> NULL. Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1) space?
- The best algorithm for the problem takes θ(n) time in the worst case.
- The best algorithm for the problem takes θ(n log n) time in the worst case.
- The best algorithm for the problem takes θ(n²) time in the worst case.
- It is not possible to reverse a singly linked list in O(1) space.
Correct answer: The best algorithm for the problem takes θ(n) time in the worst case.
Solution
Reversing a singly linked list requires visiting each node exactly once to change the pointers, which results in a linear time complexity of θ(n). This is efficient and feasible within O(1) space, as it only requires a few additional pointers for the reversal process.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →