LinkedList, Easy
Question
Reverse a singly linked list.
Example:
|
|
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
My Answer
|
|
Running time: O(n)
Space: O(1)
Recursive Answer
The recursive version is slightly trickier and the key is to work backwards. Assume that the rest of the list had already been reversed, now how do I reverse the front part? Let’s assume the list is: n1 → … → nk-1 → nk → nk+1→ … → nm → Ø
Assume from node nk+1 to nm had been reversed and you are at node nk.
n1 → … → nk-1 → nk → nk+1 ← … ← nm
We want nk+1’s next node to point to nk.
So,
nk.next.next = nk;
Be very careful that n1’s next must point to Ø. If you forget about this, your linked list has a cycle in it. This bug could be caught if you test your code with a linked list of size 2.
|
|
Complexity analysis
- Time complexity : O(n). Assume that nn is the list’s length, the time complexity is O(n).
- Space complexity : O(n). The extra space comes from implicit stack space due to recursion. The recursion could go up to n levels deep.