This problem is taken from Reverse Linked List. Here I present recursive and iterative solutions.
Recursive Solution¶
The intuition for the recursive solution is as follows: Suppose our list looks like this
1 |
|
where the first element of the linked list is called head
and the the bracketed portion of the linked list, which I call the rest.
The way we construct a reversed linked list from the head
and the rest by the following process:
First, run the reverse function on the rest. rev_head
will point to the head of the already reversed portion of the list, and rev_tail
will point to the tail of this portion.
1 2 3 |
|
Then place the rest in front of the head
by assigning rev_tail.next = head
and head.next = None
.
1 2 3 |
|
The list is now fully reversed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Iterative Solution¶
In the iterative solution, the intuition is to itertively take the head off the original linked list and make it the head of another linked list using the insert function for linked lists.
1 2 3 4 |
|
Here’s a worked example that lends some intuition to the process, starting with a normal linked list.
1 2 3 |
|
We then increment head
and use ptr
to make the head of the original linked list the head of a new linked list.
1 2 3 4 5 6 7 8 |
|
And so on, with the remaning nodes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Then return ptr
and you get your reversed linked list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|