Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Reorder List

LeetCode #143Medium
Middle + reverse + alternate merge

Given the head of a singly linked list, reorder it in place by interleaving nodes from the front and back so the order becomes first, last, second, second-to-last, and so on. Only the node links may be changed, not the values.

Asked atAmazonMicrosoftBloomberg
step 1 / 16
1
2
3
4
5
[0][1][2][3][4]
Brute force · array of node references
1given head
2collect node refs into nodes[]
3L = 0, R = n − 1
4while L < R: append nodes[L++]
5 append nodes[R−−]
6relink in that order
state
  • goal1 5 2 4 3

line 1Reorder to: first, LAST, second, SECOND-LAST, … — an outside-in weave. The pain: lists cannot walk backward to fetch the tail. Brute fix: collect node references into an array first.