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
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())Node. Its methods are stored, but none of them run yet.class Node belongs to it.__init__ is stored in Node. It runs only when it is called on an object.Node is now defined, ready to make objects from.LinkedList. Its methods are stored, but none of them run yet.class LinkedList belongs to it.__init__ is stored in LinkedList. It runs only when it is called on an object.push_front is stored in LinkedList. It runs only when it is called on an object.to_list is stored in LinkedList. It runs only when it is called on an object.LinkedList is now defined, ready to make objects from.__init__(), with self = {}. It gets its own names.head with the value null is added to the LinkedList that self points to.__init__() has finished, so its names are gone. The value it returned, {head: null}, is stored in chain.push_front(), with self = {head: null} and value = 3. It gets its own names.__init__(), with self = {} and value = 3. It gets its own names.value with the value 3 is added to the Node that self points to.next with the value null is added to the Node that self points to.__init__() has finished, so its names are gone. The value it returned, {value: 3, next: null}, is stored in node.push_front() has finished, so its names are gone.push_front(), with self = {head: a Node} and value = 2. It gets its own names.__init__(), with self = {} and value = 2. It gets its own names.value with the value 2 is added to the Node that self points to.next with the value null is added to the Node that self points to.__init__() has finished, so its names are gone. The value it returned, {value: 2, next: null}, is stored in node.next in the Node that node points to changes from null to a Node.head in the LinkedList that chain and self point to changes from a Node to a Node.push_front() has finished, so its names are gone.push_front(), with self = {head: a Node} and value = 1. It gets its own names.__init__(), with self = {} and value = 1. It gets its own names.value with the value 1 is added to the Node that self points to.next with the value null is added to the Node that self points to.__init__() has finished, so its names are gone. The value it returned, {value: 1, next: null}, is stored in node.next in the Node that node points to changes from null to a Node.push_front() has finished, so its names are gone.to_list(), with self = {head: a Node}. It gets its own names.values is created and points to a new list: [].current is created and points to a new Node: {value: 1, next: a Node}.current is not None is true, so the loop body runs.values points to.current follows the arrow to the next node.current is not None is true, so the loop body runs.values points to.current now points to a different Node: {value: 3, next: null}.current is not None is true, so the loop body runs.values points to.current is set to null, so it no longer points to any object.current is None: the end of the chain.values.to_list() has finished, so its names are gone. It prints [1, 2, 3].Answer: Walk from head: O(n)
Answer: None
← Quick Sort · Reversing a Linked List →