61. 旋转链表
61. 旋转链表
题目
描述:
给你一个链表的头节点 head,旋转链表,将链表每个节点向右移动 k 个位置。
示例 1:
- 输入:
head = [1,2,3,4,5],k = 2 - 输出:
[4,5,1,2,3]
示例 2:
- 输入:
head = [0,1,2],k = 4 - 输出:
[2,0,1]
提示:
- 链表中节点的数目在范围
[0, 500]内 -100 <= Node.val <= 1000 <= k <= 2 * 10^9
题解
先计算真正需要旋转的步数
设链表长度为 n。向右旋转一次,就是把最后一个节点移到最前面;旋转 n 次会恢复原顺序,因此只需处理 r = k % n 次。这样即使 k 很大,也不用逐次移动节点。
空链表、单节点链表和 k = 0 可直接返回,尤其要先处理空链表,避免对零长度取模。遍历得到长度和尾节点后,如果 r = 0,也直接返回。
连成环后寻找新尾节点
旋转 r 次相当于把最后 r 个节点整体移到前面,所以新尾是原链表的第 n - r 个节点。令旧尾的 next 指向旧头,先形成环;再从旧尾出发走 n - r 步,就能到达新尾。
例如 [1,2,3,4,5]、k = 2,n - r = 3。成环后从旧尾 5 依次走到 1、2、3,新尾为 3,新头为 4。先保存新头,再把 3 的 next 置空,结果为 [4,5,1,2,3]。
核心代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0) return head;
int n = 1;
ListNode tail = head;
while (tail.next != null) {
tail = tail.next;
n++;
}
int move = k % n;
if (move == 0) return head;
tail.next = head;
int steps = n - move;
while (steps-- > 0) {
tail = tail.next;
}
ListNode newHead = tail.next;
tail.next = null;
return newHead;
}
核心代码用 tail 寻找新尾,下面完整示例用 iter,两者移动规则相同。最后的 next = null 必须执行,否则返回的仍是环,普通的链表遍历就无法结束。
统计长度和寻找断点各扫描至多一遍链表,时间为 $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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public class Main {
public static 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 static ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0) {
return head;
}
ListNode iter = head;
int n = 1;
while (iter.next != null) {
iter = iter.next;
n++;
}
int steps = n - k % n;
if (steps == n) {
return head;
}
iter.next = head;
while (steps-- > 0) {
iter = iter.next;
}
ListNode newHead = iter.next;
iter.next = null;
return newHead;
}
public static ListNode createList(int[] arr) {
if (arr.length == 0) {
return null;
}
ListNode head = new ListNode(arr[0]);
ListNode curr = head;
for (int i = 1; i < arr.length; i++) {
curr.next = new ListNode(arr[i]);
curr = curr.next;
}
return head;
}
public static void printList(ListNode head) {
StringBuilder sb = new StringBuilder();
sb.append("[");
ListNode cur = head;
while (cur != null) {
sb.append(cur.val);
if (cur.next != null) {
sb.append(", ");
}
cur = cur.next;
}
sb.append("]");
System.out.println(sb.toString());
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int k1 = 2;
ListNode head1 = createList(arr1);
System.out.println("示例 1 输入:");
System.out.print("head = ");
printList(head1);
System.out.println("k = " + k1);
ListNode res1 = rotateRight(head1, k1);
System.out.print("示例 1 输出: ");
printList(res1);
System.out.println("-----------------------------");
int[] arr2 = {0, 1, 2};
int k2 = 4;
ListNode head2 = createList(arr2);
System.out.println("示例 2 输入:");
System.out.print("head = ");
printList(head2);
System.out.println("k = " + k2);
ListNode res2 = rotateRight(head2, k2);
System.out.print("示例 2 输出: ");
printList(res2);
System.out.println("-----------------------------");
int[] arr3 = {1, 2};
int k3 = 2;
ListNode head3 = createList(arr3);
System.out.println("示例 3 输入:");
System.out.print("head = ");
printList(head3);
System.out.println("k = " + k3);
ListNode res3 = rotateRight(head3, k3);
System.out.print("示例 3 输出: ");
printList(res3);
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
示例 1 输入:
head = [1, 2, 3, 4, 5]
k = 2
示例 1 输出: [4, 5, 1, 2, 3]
-----------------------------
示例 2 输入:
head = [0, 1, 2]
k = 4
示例 2 输出: [2, 0, 1]
-----------------------------
示例 3 输入:
head = [1, 2]
k = 2
示例 3 输出: [1, 2]
本文由作者按照 CC BY 4.0 进行授权


