Learn / Linked Lists

Intermediate 14 min

Linked Lists

Nodes, pointers, singly vs doubly linked, and tradeoffs vs arrays.

What you will learn

  • Describe a node structure
  • Insert at head in O(1)
  • Compare to arrays
python
class Node:
    def __init__(self, val, nxt=None):
        self.val = val
        self.next = nxt

head = Node(1, Node(2, Node(3)))
cur = head
while cur:
    print(cur.val)
    cur = cur.next

Try it yourself

Implement a function to count nodes in a singly linked list.