Linked lists

Draw the nodes before you write the code. Every bug on this page is a link assigned in the wrong order; the dummy head and saving next before overwriting it fix most of them.

The lesson is Linked lists.

Read a linked list into a Python list

Write values(head) that returns the values of the linked list starting at head as a Python list, in order. head is a ListNode or None.

values(1 -> 2 -> 3)  ->  [1, 2, 3]
values(None)         ->  []

The point is to practise the walk. The checker has a helper that does this; write your own.

def values(head):
    ...

Build a linked list

Write build(items) that creates a linked list of ListNodes holding the values of items in order and returns its head, or None for an empty list. Import ListNode from checker.

build([1, 2, 3])  ->  1 -> 2 -> 3
build([])         ->  None

The point is to practise creating and linking nodes with a tail reference, or with a dummy head.

def build(items):
    ...

n-th node from the end

Write nth_from_end(head, n) that returns the value of the n-th node counting from the end, where n = 1 is the last node. n is at most the length.

nth_from_end(1 -> 2 -> 3 -> 4 -> 5, 2)  ->  4
nth_from_end(1 -> 2 -> 3 -> 4 -> 5, 5)  ->  1

The point is to practise two pointers a fixed distance apart, in one pass.

def nth_from_end(head, n):
    ...

Remove duplicates from a sorted linked list

Write dedupe(head) that removes repeated values from a sorted linked list so each value appears once, relinking the existing nodes, and returns the head.

dedupe(1 -> 1 -> 2 -> 3 -> 3)  ->  1 -> 2 -> 3
dedupe(None)                   ->  None

The point is to practise comparing a node with its successor and skipping.

def dedupe(head):
    ...

Does the list loop?

A linked list may have a cycle: some node's next points back to an earlier node, so walking it never ends. Write has_cycle(head) that returns whether it does, without using extra memory proportional to the length.

has_cycle(1 -> 2 -> 3 -> back to 2)  ->  True
has_cycle(1 -> 2 -> 3)               ->  False

The point is to practise fast and slow pointers: in a cycle, the fast one laps the slow one.

def has_cycle(head):
    ...

Is the list a palindrome?

Write is_palindrome(head) that returns whether the linked list reads the same forwards and backwards.

is_palindrome(1 -> 2 -> 2 -> 1)  ->  True
is_palindrome(1 -> 2)            ->  False
is_palindrome(None)              ->  True

The point is to practise combining the techniques: find the middle, reverse the second half, compare. Reading the values into a Python list also passes.

def is_palindrome(head):
    ...

Rotate a linked list

Write rotate_right(head, k) that moves the last k nodes to the front, returning the new head. k may be larger than the length, in which case it wraps around.

rotate_right(1 -> 2 -> 3 -> 4 -> 5, 2)  ->  4 -> 5 -> 1 -> 2 -> 3
rotate_right(1 -> 2, 3)                 ->  2 -> 1
rotate_right(None, 4)                   ->  None

The point is to practise finding a split point and relinking the ends.

def rotate_right(head, k):
    ...

Add two numbers stored in reverse

Two non-negative numbers are stored as linked lists of digits, least significant digit first: 342 is 2 -> 4 -> 3. Write add(a, b) that returns their sum as a new linked list in the same form.

add(2 -> 4 -> 3, 5 -> 6 -> 4)  ->  7 -> 0 -> 8       (342 + 465 = 807)
add(9 -> 9, 1)                 ->  0 -> 0 -> 1       (99 + 1 = 100)

The point is to practise walking two lists together with a carry, and building the result with a tail pointer.

def add(a, b):
    ...

Swap nodes in pairs

Write swap_pairs(head) that swaps every two adjacent nodes, relinking rather than changing values, and returns the new head. A leftover node at the end stays put.

swap_pairs(1 -> 2 -> 3 -> 4)  ->  2 -> 1 -> 4 -> 3
swap_pairs(1 -> 2 -> 3)       ->  2 -> 1 -> 3
swap_pairs(None)              ->  None

The point is to practise relinking three nodes at a time with a dummy head.

def swap_pairs(head):
    ...

Partition around a value

Write partition(head, x) that rearranges the linked list so every node with a value less than x comes before every node with a value at least x, preserving the original order within each group, and returns the head.

partition(1 -> 4 -> 3 -> 2 -> 5 -> 2, 3)  ->  1 -> 2 -> 2 -> 4 -> 3 -> 5
partition(2 -> 1, 2)                      ->  1 -> 2

The point is to practise building two lists with two tails and joining them.

def partition(head, x):
    ...

Next: Trees.