head = [1,2,3,4,5][1,5,2,4,3]Rearrange a singly linked list by taking its first node, then its last node, then its second node, then its second-to-last node, and so on. Change next links in place. Keep each node and its value.
head = [1,2,3,4,5][1,5,2,4,3]head = [1,2,3,4][1,4,2,3]Output shows the list after reordering. The function returns None, not a new list. The task allows 1 to 50,000 nodes with values from 1 to 1000.
LeetCode 143 ↗head = [1,2,3,4,5] output: [1,5,2,4,3]
Collecting and reconnecting the N nodes takes linear time.
The extra list stores N node references.
head = [1,2,3,4,5] output: [1,5,2,4,3]
Both approaches take O(N) time. Splitting, reversing, and weaving avoids the extra reference list and reduces space complexity from O(N) to O(1).
Finding the middle, reversing half the list, and weaving the halves each take linear time. Their total is O(N).
Only a fixed number of node references is used. The reversal is iterative, with no recursion stack.