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

the reversed linked list should look like

Which one of the following statements is about the time complexity of algorithms that solve the above problem in space?
View worked solution
Answer: A
To reverse a singly linked list of size in-place (using auxiliary space), we can use an iterative approach with three pointers: prev, curr, and next.
Iterative Algorithm:
- Initialize three pointers:
prev = NULL,curr = head, andnext = NULL. - Iterate through the linked list until
currisNULL:- Store the next node:
next = curr->next - Reverse the current node's pointer:
curr->next = prev - Move pointers one step forward:
prev = curr,curr = next
- Store the next node:
- Set the head of the reversed list:
head = prev.
Complexity Analysis:
- Time Complexity: The algorithm traverses the list of nodes exactly once. At each node, it performs a constant number of pointer updates. Thus, the worst-case time complexity is .
- Space Complexity: The algorithm only uses three extra pointer variables, which requires auxiliary space.
Since we can successfully reverse the list in time and space, Option A is the correct statement.
Free practice · Your answers on this page are not saved.
