Data structures · 10 / 15 Exercise Hard

Linked list

Objective

Create a Python program that implements a singly linked list with a Node class (value and next) and a LinkedList class (head), and operates it with commands.

Instructions

  • Read an integer n and then n commands. Values are integers. Do not use Python lists to store the data: link nodes.
  • append X and prepend X add a node at the end or at the beginning (showing nothing).
  • remove X: removes the first occurrence of X and shows Removed: X, or Not found: X if it is not there.
  • reverse: reverses the list by changing the links (showing nothing).
  • show: shows the values separated by -> , for example 1 -> 2 -> 3, or Empty list if it is empty.
Input
8
append 2
append 3
prepend 1
show
remove 2
reverse
show
remove 9
Output
1 -> 2 -> 3
Removed: 2
3 -> 1
Not found: 9

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise