All problems
Reverse a Singly Linked List
Problem
Reverse a singly linked list represented as a Python list of values.
Build the linked list in order, reverse the links so the tail becomes the head, and return the values from the new head to the tail.
An empty list returns an empty list.
Hint
Walk the nodes once, and prepend each value to a new list to simulate rerouting the links.
Test cases
case 1
in: [[1,2,3,4,5]]
expected: [5,4,3,2,1]
case 2
in: [[]]
expected: []
case 3
in: [[1]]
expected: [1]
case 4
in: [[1,2]]
expected: [2,1]
Starter codePython
def reverse_list(values):
# Your code here
pass4 test cases · run your code against them in the editor