This interactive note demonstrates how a singly linked list is reversed using Python.
import%20marimo%20as%20mo%0A%0A%0Aclass%20Node%3A%0A%20%20%20%20def%20__init__(self%2C%20value%2C%20next%3DNone)%3A%0A%20%20%20%20%20%20%20%20self.value%20%3D%20value%0A%20%20%20%20%20%20%20%20self.next%20%3D%20next%0A%0A%20%20%20%20def%20__str__(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22Node(%7Bself.value%7D)%22%0A%0A%0Adef%20linked_list_to_mermaid(head%3A%20Node)%20-%3E%20str%3A%0A%20%20%20%20if%20not%20head%3A%0A%20%20%20%20%20%20%20%20return%20%22graph%20LR%5Cn%20%20%20%20empty%5B(empty)%5D%22%0A%0A%20%20%20%20lines%20%3D%20%5B%22graph%20LR%22%5D%0A%20%20%20%20node_defs%20%3D%20%5B%5D%0A%20%20%20%20edges%20%3D%20%5B%5D%0A%0A%20%20%20%20idx%20%3D%200%0A%20%20%20%20current%20%3D%20head%0A%0A%20%20%20%20while%20current%3A%0A%20%20%20%20%20%20%20%20node_defs.append(f%22%20%20%20%20node%7Bidx%7D%5B%7Bcurrent.value%7D%5D%22)%0A%20%20%20%20%20%20%20%20if%20current.next%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20edges.append(f%22%20%20%20%20node%7Bidx%7D%20--%3E%20node%7Bidx%20%2B%201%7D%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20edges.append(f%22%20%20%20%20node%7Bidx%7D%20--%3E%20none((None))%22)%0A%20%20%20%20%20%20%20%20current%20%3D%20current.next%0A%20%20%20%20%20%20%20%20idx%20%2B%3D%201%0A%0A%20%20%20%20return%20%22%5Cn%22.join(lines%20%2B%20node_defs%20%2B%20edges)%0A%0A%0Adef%20get_last_node(head%3A%20Node)%3A%0A%20%20%20%20node%20%3D%20head%0A%20%20%20%20while%20node.next%3A%0A%20%20%20%20%20%20%20%20node%20%3D%20node.next%0A%20%20%20%20return%20node
numbers_input%20%3D%20mo.ui.text(label%3D%22Enter%20a%20list%20of%20numbers%20(comma-separated)%22%2C%20value%3D%221%2C2%2C3%2C4%22)%0Anumbers_input
Here is a visual representation of the linked list:
def%20parse_linked_list(text%3A%20str)%20-%3E%20Node%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20nums%20%3D%20%5Bint(x.strip())%20for%20x%20in%20text.split(%22%2C%22)%20if%20x.strip()%5D%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20return%20None%20%20%23%20Invalid%20input%0A%0A%20%20%20%20if%20not%20nums%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%0A%20%20%20%20head%20%3D%20Node(nums%5B0%5D)%0A%20%20%20%20current%20%3D%20head%0A%20%20%20%20for%20num%20in%20nums%5B1%3A%5D%3A%0A%20%20%20%20%20%20%20%20current.next%20%3D%20Node(num)%0A%20%20%20%20%20%20%20%20current%20%3D%20current.next%0A%0A%20%20%20%20return%20head%0A%0Ahead%20%3D%20parse_linked_list(numbers_input.value)%0Adiagram%20%3D%20linked_list_to_mermaid(head)%0Amo.mermaid(diagram)
The diagram after applying the reverse function looks like this:
CODE%20%3D%20%22%22%22def%20reverse(head%3A%20Node)%3A%0A%20%20%20%20previous%20%3D%20None%0A%20%20%20%20current%20%3D%20head%0A%20%20%20%20while%20current%20!%3D%20None%3A%0A%20%20%20%20%20%20%20%20next%20%3D%20current.next%0A%20%20%20%20%20%20%20%20current.next%20%3D%20previous%0A%20%20%20%20%20%20%20%20previous%20%3D%20current%0A%20%20%20%20%20%20%20%20current%20%3D%20next%22%22%22%0Acode_editor%20%3D%20mo.ui.code_editor(CODE%2C%20theme%3D%22dark%22%2C%20show_copy_button%3DFalse)%0Acode_editor
import%20traceback%0A%0A%0Areverse%20%3D%20None%0Awith%20mo.redirect_stderr()%2C%20mo.redirect_stdout()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20exec(code_editor.value)%0A%20%20%20%20except%20Exception%20as%20e%3A%0A%20%20%20%20%20%20%20%20print(%22Errors%20defining%20the%20function%3A%22)%0A%20%20%20%20%20%20%20%20traceback.print_exc()
import%20copy%0A%0Amo.stop(reverse%20is%20None)%0Ahead_copy%20%3D%20copy.deepcopy(head)%0Atail%20%3D%20get_last_node(head_copy)%0Aok%20%3D%20True%0Awith%20mo.redirect_stderr()%2C%20mo.redirect_stdout()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20reverse(head_copy)%0A%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20print(%22Errors%20running%20the%20function%3A%22)%0A%20%20%20%20%20%20%20%20traceback.print_exc()%0A%20%20%20%20%20%20%20%20ok%20%3D%20False
mo.stop(not%20ok)%0Adiagram_reverse%20%3D%20linked_list_to_mermaid(tail)%0Amo.mermaid(diagram_reverse)
You can edit the code in the reverse function cell to experiment with different logic.
The diagram will update automatically to reflect the result 🙌🏽