Linked Lists

Chain nodes together with next pointers, add to the front in O(1), and walk the chain from head to the end.

Python · intermediate · 8 min · 52 steps · free

What you'll learn

linked_list.py

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def push_front(self, value):
        node = Node(value)
        node.next = self.head
        self.head = node

    def to_list(self):
        values = []
        current = self.head
        while current is not None:
            values.append(current.value)
            current = current.next
        return values

chain = LinkedList()
chain.push_front(3)
chain.push_front(2)
chain.push_front(1)
print(chain.to_list())

Step by step

  1. Python starts building the class Node. Its methods are stored, but none of them run yet.
  2. Python reads the class body: everything indented under class Node belongs to it.
  3. The method __init__ is stored in Node. It runs only when it is called on an object.
  4. The class Node is now defined, ready to make objects from.
  5. Python starts building the class LinkedList. Its methods are stored, but none of them run yet.
  6. Python reads the class body: everything indented under class LinkedList belongs to it.
  7. The method __init__ is stored in LinkedList. It runs only when it is called on an object.
  8. The method push_front is stored in LinkedList. It runs only when it is called on an object.
  9. The method to_list is stored in LinkedList. It runs only when it is called on an object.
  10. The class LinkedList is now defined, ready to make objects from.
  11. Python calls __init__(), with self = {}. It gets its own names.
  12. A new key head with the value null is added to the LinkedList that self points to.
  13. __init__() has finished, so its names are gone. The value it returned, {head: null}, is stored in chain.
  14. Python calls push_front(), with self = {head: null} and value = 3. It gets its own names.
  15. Python calls __init__(), with self = {} and value = 3. It gets its own names.
  16. A new key value with the value 3 is added to the Node that self points to.
  17. A new key next with the value null is added to the Node that self points to.
  18. __init__() has finished, so its names are gone. The value it returned, {value: 3, next: null}, is stored in node.
  19. The new node points to the old head (None for now)…
  20. …and head now points to the new node. Follow the arrows in the Objects panel.
  21. push_front() has finished, so its names are gone.
  22. Python calls push_front(), with self = {head: a Node} and value = 2. It gets its own names.
  23. Python calls __init__(), with self = {} and value = 2. It gets its own names.
  24. A new key value with the value 2 is added to the Node that self points to.
  25. A new key next with the value null is added to the Node that self points to.
  26. __init__() has finished, so its names are gone. The value it returned, {value: 2, next: null}, is stored in node.
  27. next in the Node that node points to changes from null to a Node.
  28. head in the LinkedList that chain and self point to changes from a Node to a Node.
  29. push_front() has finished, so its names are gone.
  30. Python calls push_front(), with self = {head: a Node} and value = 1. It gets its own names.
  31. Python calls __init__(), with self = {} and value = 1. It gets its own names.
  32. A new key value with the value 1 is added to the Node that self points to.
  33. A new key next with the value null is added to the Node that self points to.
  34. __init__() has finished, so its names are gone. The value it returned, {value: 1, next: null}, is stored in node.
  35. next in the Node that node points to changes from null to a Node.
  36. Head points to 1, which points to 2, which points to 3.
  37. push_front() has finished, so its names are gone.
  38. Python calls to_list(), with self = {head: a Node}. It gets its own names.
  39. values is created and points to a new list: [].
  40. current is created and points to a new Node: {value: 1, next: a Node}.
  41. current is not None is true, so the loop body runs.
  42. 1 is added to the end of the list that values points to.
  43. current follows the arrow to the next node.
  44. current is not None is true, so the loop body runs.
  45. 2 is added to the end of the list that values points to.
  46. current now points to a different Node: {value: 3, next: null}.
  47. current is not None is true, so the loop body runs.
  48. 3 is added to the end of the list that values points to.
  49. current is set to null, so it no longer points to any object.
  50. current is None: the end of the chain.
  51. The function returns values.
  52. to_list() has finished, so its names are gone. It prints [1, 2, 3].

Common mistakes

Best practices

Key takeaways

Check yourself

Get the 3rd item of a linked list?

Answer: Walk from head: O(n)

The last node's next is?

Answer: None

·

More in Lists, Stacks, Queues and Hashing