Practice › Linked List
Reorder a List
Medium · Linked List · Asked at Amazon, Meta, Microsoft, Adobe
For a list L0 → L1 → … → L(n−1), rearrange the nodes into L0 → L(n−1) → L1 → L(n−2) → L2 → … and print the list.
Input
First line: n. Second line: the n node values from head to tail.
Output
The reordered list, space-separated.
Constraints
- 1 ≤ n ≤ 10^5
- −10^9 ≤ values ≤ 10^9
Three classic steps: find the middle (slow/fast pointers), reverse the second half, then merge the two halves by alternating nodes.
Example 1
Input
4 1 2 3 4
Output
1 4 2 3
Example 2
Input
5 1 2 3 4 5
Output
1 5 2 4 3
Hints
- The answer alternates between the front half and the back half read backwards.
- Find the middle with slow and fast pointers, then cut the list there.
- Reverse the second half and merge the two halves one node at a time.
Approach
- slow/fast pointers stop slow at the end of the first half (for odd n the middle node stays in the first half). 2) Cut: second = slow.next, slow.next = null, and reverse second. 3) Merge: take one node from the first half, then one from the reversed second half, until the second half runs out. All in place, without an array.
Complexity: O(n) time · O(1) extra space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
struct Node {
long long val;
Node *next = nullptr;
Node(long long v) : val(v) {}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
Node dummy(0), *t = &dummy;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
t = t->next = new Node(x);
}
Node *head = dummy.next, *slow = head, *fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
Node *second = slow->next, *prev = nullptr;
slow->next = nullptr;
while (second) {
Node *nx = second->next;
second->next = prev;
prev = second;
second = nx;
}
for (Node *a = head, *b = prev; b;) {
Node *an = a->next, *bn = b->next;
a->next = b;
b->next = an;
a = an;
b = bn;
}
for (Node *p = head; p; p = p->next) cout << p->val << (p->next ? " " : "\n");
return 0;
}Python solution
import sys
class Node:
def __init__(self, val):
self.val, self.next = val, None
data = list(map(int, sys.stdin.read().split()))
dummy = tail = Node(0)
for v in data[1 : 1 + data[0]]:
tail.next = Node(v)
tail = tail.next
head = slow = fast = dummy.next
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
second, prev = slow.next, None
slow.next = None
while second:
nxt = second.next
second.next = prev
prev, second = second, nxt
a, b = head, prev
while b:
an, bn = a.next, b.next
a.next = b
b.next = an
a, b = an, bn
out, p = [], head
while p:
out.append(p.val)
p = p.next
print(*out)Java solution
import java.io.*;
public class Main {
static class Node {
long val;
Node next;
Node(long v) { val = v; }
}
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
Node dummy = new Node(0), t = dummy;
for (int i = 0; i < n; i++) {
in.nextToken();
t = t.next = new Node((long) in.nval);
}
Node head = dummy.next, slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Node second = slow.next, prev = null;
slow.next = null;
while (second != null) {
Node nx = second.next;
second.next = prev;
prev = second;
second = nx;
}
Node a = head, b = prev;
while (b != null) {
Node an = a.next, bn = b.next;
a.next = b;
b.next = an;
a = an;
b = bn;
}
StringBuilder sb = new StringBuilder();
for (Node p = head; p != null; p = p.next) sb.append(p.val).append(p.next != null ? " " : "\n");
System.out.print(sb);
}
}