Problems › Linked lists

Reverse a Linked List

Difficulty: Easy · Topic: Linked lists

A linked list is given as a list of values vals in head-to-tail order. Return the values reversed. With real nodes you'd rewire each next pointer to the previous node as you walk; here, produce the reversed sequence.

Examples

reverse_list([1,2,3])
→ [3,2,1]
reverse_list([1])
→ [1]

Target complexity: time O(n), space O(n)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Linked lists problems