LeetCode 493 Reverse Pairs - Implementation

The approach

Use modified merge sort. During the merge step, count pairs where an element from the left half is more than twice an element from the right half. The total count is sum of counts from both halves plus the merge step.

Here's the solution:

function reverseList(head):
    prev = null
    curr = head
    while curr:
        next = curr.next
        curr.next = prev
        prev = curr
        curr = next
    return prev

O(nlogn)O(n \log n) time, O(n)O(n) space.