Practice mode Data Structures

What is the time complexity of reversing a singly linked list of n nodes in place?

Correct answer B. O(n)

Explanation

In-place reversal walks the list once with three pointers (previous, current and next), flipping each node's next link to point backwards. Every node is visited exactly once, so the time is O(n) and the extra space is O(1). It cannot be O(1) time because each of the n links has to change; quadratic time would arise only from a naive repeated-traversal approach.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.