Quant Memo
Coding/●●●●●

Reverse only a sublist of a linked list

Given the head of a singly linked list and two 1-indexed positions m <= n, reverse the nodes from position m to position n inclusive and return the head. The nodes outside [m, n] keep their order.

1 -> 2 -> 3 -> 4 -> 5,  m = 2, n = 4
becomes
1 -> 4 -> 3 -> 2 -> 5

Do it in one pass, O(n)O(n) time and O(1)O(1) extra space. Rebuilding from a Python list is not accepted; this is pointer surgery on a sub-range.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions