Reverse a linked list

Write reverse(head) that reverses the linked list in place by re-pointing each node's next, and returns the new head. head is a ListNode or None.

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

The point is to practise pointer manipulation with three references: previous, current and next. Relink the existing nodes rather than building a new list.

def reverse(head):
    ...