How to Reverse a singly-linked list in Python
The challenge
Implement a function reverse_list
that takes a singly-linked list of nodes and returns a matching list in the reverse order.
Assume the presence of a class Node
, which exposes the property value
/Value
and next
/Next
. next
must either be set to the next Node
in the list, or to None
(or null
) to indicate the end of the list.
To assist in writing tests, a function make_linked_list
(Node.asLinkedList()
in Java) has also been defined, which converts a python list to a linked list of Node
.
The final tests will use a very long list. Be aware that a recursive solution will run out of stack
.
The solution in Python code
Option 1:
Option 2:
Option 3:
Test cases to validate our solution
|
|