Merge two sorted linked lists

a and b are the heads of two linked lists whose values are in ascending order. Write merge(a, b) that returns the head of one sorted linked list containing all the nodes from both. Either input may be None.

merge(1 -> 3 -> 5, 2 -> 3 -> 6)  ->  1 -> 2 -> 3 -> 3 -> 5 -> 6
merge(None, 4 -> 7)              ->  4 -> 7

The point is to practise the merge step on nodes: relink the existing nodes rather than copying values into a new list.

def merge(a, b):
    ...