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):
    ...