归档

92. 反转链表II

92. 反转链表II

题目

描述:

给你单链表的头指针 head 和两个整数 left 和 right,其中 left <= right。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表。

示例 1:

示例1图片

  • 输入: head = [1,2,3,4,5], left = 2, right = 4
  • 输出: [1,4,3,2,5]

示例 2:

  • 输入: head = [5], left = 1, right = 1
  • 输出: [5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?

题解

用头插法逐步反转区间

位置从 1 开始计数。反转 [left, right] 时,区间外的节点顺序不变,只需调整区间内部的 next 指针。先在头节点前加一个虚拟头节点 dummy,这样即使 left = 1,待反转区间也有前驱。

让 pre 从 dummy 向前走 left - 1 步,停在区间前驱;让 cur = pre.next,指向区间原来的第一个节点。接下来 pre 和 cur 都不再移动:pre 始终是区间前驱,cur 始终是原区间首节点。每轮把 cur 后面的节点取出,插到 pre 后面,即插到反转段的最前面,所以叫头插法。

例如 [1,2,3,4,5] 反转位置 2 到 4,pre 指向 1,cur 指向 2:

  1. 把 3 从 2 后面移到 1 后面,得到 [1,3,2,4,5]。
  2. 把 4 从 2 后面移到 1 后面,得到 [1,4,3,2,5]。

原区间有 right - left + 1 个节点,第一个节点已经就位,因此执行 right - left 次头插即可。

一轮操作中的指针顺序

先用 next 保存 cur.next,再让 cur.next = next.next 跳过它;接着让 next.next 指向当前区间头 pre.next,最后更新 pre.next = next。这四步保证后续节点始终可达,也让原区间首节点 cur 逐渐成为反转段的尾节点。

核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ListNode reverseBetween(ListNode head, int left, int right) {
    if (head == null || left == right) return head;

    ListNode dummy = new ListNode(0, head);
    ListNode pre = dummy;
    for (int i = 1; i < left; i++) {
        pre = pre.next;
    }

    ListNode cur = pre.next;
    for (int i = 0; i < right - left; i++) {
        ListNode next = cur.next;
        cur.next = next.next;
        next.next = pre.next;
        pre.next = next;
    }

    return dummy.next;
}

left = right 时循环执行零次,链表保持原样。返回 dummy.next,可以同时覆盖原头节点保留和发生变化的情况。

定位前驱和反转区间都只向前扫描,时间复杂度为 $O(n)$;只使用固定数量的指针,辅助空间为 $O(1)$。

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
// 链表节点定义
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
public class Main {
    public static ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }
        ListNode cur = pre.next;
        for (int i = 0; i < right - left; i++) {
            ListNode next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return dummy.next;
    }
    public static void printList(ListNode head) {
        ListNode curr = head;
        System.out.print("[");
        while (curr != null) {
            System.out.print(curr.val);
            if (curr.next != null) {
                System.out.print(", ");
            }
            curr = curr.next;
        }
        System.out.println("]");
    }
    public static ListNode buildList(int[] vals) {
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        for (int val : vals) {
            cur.next = new ListNode(val);
            cur = cur.next;
        }
        return dummy.next;
    }
    public static void main(String[] args) {
        int[] input1 = {1, 2, 3, 4, 5};
        ListNode head1 = buildList(input1);
        int left1 = 2, right1 = 4;
        System.out.println("示例 1 输入: head = " + Arrays.toString(input1) + ", left = " + left1 + ", right = " + right1);
        ListNode res1 = reverseBetween(head1, left1, right1);
        System.out.print("示例 1 输出: ");
        printList(res1);
        System.out.println("-----------------------------");
        int[] input2 = {5};
        ListNode head2 = buildList(input2);
        int left2 = 1, right2 = 1;
        System.out.println("示例 2 输入: head = " + Arrays.toString(input2) + ", left = " + left2 + ", right = " + right2);
        ListNode res2 = reverseBetween(head2, left2, right2);
        System.out.print("示例 2 输出: ");
        printList(res2);
    }
}

运行结果(OpenJDK 21.0.12):

1
2
3
4
5
示例 1 输入: head = [1, 2, 3, 4, 5], left = 2, right = 4
示例 1 输出: [1, 4, 3, 2, 5]
-----------------------------
示例 2 输入: head = [5], left = 1, right = 1
示例 2 输出: [5]
本文由作者按照 CC BY 4.0 进行授权