Practice › Linked List
Reverse a Sublist
Medium · Linked List · Asked at Amazon, Microsoft, Meta, Apple
Reverse only the nodes from position l to position r (1-based, inclusive) and print the whole list.
Input
First line: n. Second line: the n node values from head to tail. Third line: l r.
Output
The list after reversing the part from l to r, space-separated.
Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ l ≤ r ≤ n
- −10^9 ≤ values ≤ 10^9
Walk to the node just before position l. Then, r − l times, take the node after the current one and move it to the front of the reversed part — one pass, no extra list.
Example 1
Input
5 1 2 3 4 5 2 4
Output
1 4 3 2 5
Example 2
Input
1 5 1 1
Output
5
Hints
- Walk to the node just before position l — call it prev (a dummy head helps when l = 1).
- Let cur = prev.next. It will end up at position r.
- r − l times, take the node after cur and move it to just after prev.
Approach
This "head insertion" reverses the part in one pass. Each step: move = cur.next; cur.next = move.next; move.next = prev.next; prev.next = move. After r − l steps, positions l…r are reversed and both ends are still connected to the rest of the list.
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);
}
int l, r;
cin >> l >> r;
Node *prev = &dummy;
for (int i = 1; i < l; i++) prev = prev->next;
Node *cur = prev->next;
for (int i = 0; i < r - l; i++) {
Node *move = cur->next;
cur->next = move->next;
move->next = prev->next;
prev->next = move;
}
for (Node *p = dummy.next; 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()))
n = data[0]
dummy = tail = Node(0)
for v in data[1 : 1 + n]:
tail.next = Node(v)
tail = tail.next
l, r = data[1 + n], data[2 + n]
prev = dummy
for _ in range(l - 1):
prev = prev.next
cur = prev.next
for _ in range(r - l):
move = cur.next
cur.next = move.next
move.next = prev.next
prev.next = move
out, p = [], dummy.next
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);
}
in.nextToken();
int l = (int) in.nval;
in.nextToken();
int r = (int) in.nval;
Node prev = dummy;
for (int i = 1; i < l; i++) prev = prev.next;
Node cur = prev.next;
for (int i = 0; i < r - l; i++) {
Node move = cur.next;
cur.next = move.next;
move.next = prev.next;
prev.next = move;
}
StringBuilder sb = new StringBuilder();
for (Node p = dummy.next; p != null; p = p.next) sb.append(p.val).append(p.next != null ? " " : "\n");
System.out.print(sb);
}
}