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, shown as ground symbol)
the reversed linked list should look like
head → [e|•]→ [d|•]→ [c|•]→ [b|•]→ [a|•]→ (null, shown as ground symbol)
Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in space?
Tap to open full sizeFigure description
Two horizontal singly linked list diagrams, each a chain of 5 boxes. Each box is drawn as a two-cell node: left cell holds a data value, right cell holds a pointer (drawn as a small vertical bar with an arrow leaving it to the next node). Top diagram (original list): 'head' with an arrow into node containing 'a', arrow to node 'b', arrow to node 'c', arrow to node 'd', arrow to node 'e', and the last node's pointer cell points to a ground/earth symbol (⏚) denoting NULL. Bottom diagram (reversed list): identical structure but with values in order 'head' → e → d → c → b → a → NULL (ground symbol). This just illustrates that the list a-b-c-d-e becomes e-d-c-b-a after reversal.
- (A)
The best algorithm for the problem takes time in the worst case.
- (B)
The best algorithm for the problem takes time in the worst case.
- (C)
The best algorithm for the problem takes time in the worst case.
- (D)
It is not possible to reverse a singly linked list in space.