AI Generated Cheatsheets

Linked List Cheatsheet

Overview

Operations

Insertion

Insertion at the Head

# create a new node
new_node = Node(value)

# point new node to the current head
new_node.next = head

# make new node the new head
head = new_node

Insertion at the Tail

# create a new node
new_node = Node(value)

# point the current tail to the new node
tail.next = new_node

# make the new node the new tail
tail = new_node

Deletion

Deletion at the Head

# point the head to the next node
head = head.next

Deletion at the Tail

# traverse the list until the node before the tail
current_node = head
while current_node.next != tail:
  current_node = current_node.next

# point the current tail to None
current_node.next = None

# make the node before the current tail the new tail
tail = current_node

Traversal

current_node = head
while current_node is not None:
  # do something with current_node.value
  current_node = current_node.next

Time Complexity

Resources