structy-logo
Pythonpython3logo
Course Contents
Layout 1
Prompt
Editor
Terminal
sign in
problem
approach
walkthrough
solution
add to favoritessettings

reverse list

Write a function, reverse_list, that takes in the head of a linked list as an argument. The function should reverse the order of the nodes in the linked list in-place and return the new head of the reversed linked list.

test_00
a = Node("a")
b = Node("b")
c = Node("c")
d = Node("d")
e = Node("e")
f = Node("f")

a.next = b
b.next = c
c.next = d
d.next = e
e.next = f

# a -> b -> c -> d -> e -> f

reverse_list(a) # f -> e -> d -> c -> b -> a
test_01
x = Node("x")
y = Node("y")

x.next = y

# x -> y

reverse_list(x) # y -> x
test_02
p = Node("p")

# p

reverse_list(p) # p
terminal
settings
[guest]$ 
editor — reverse-list.py
reset codesettings
# class Node:
# def __init__(self, val):
# self.val = val
# self.next = None

def reverse_list(head):
pass # todo

saved