ReverseNodesInKGroup

K 个一组翻转链表

题目介绍

K 个一组翻转链表

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

示例 1:

1
2
3
4
5
给你这个链表:1->2->3->4->5

当 k = 2 时,应当返回: 2->1->4->3->5

当 k = 3 时,应当返回: 3->2->1->4->5

说明:

  • 你的算法只能使用常数的额外空间。
  • 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

题目解法

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
97
98
99
100
101
102
103
104
105
package algorithm;

public class ReverseNodesInKGroup {

public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k <= 1) {
return head;
}

ListNode prev = head;
ListNode cur = head;
int length = 0;
while (length < k && cur != null) {
length++;
prev = cur;
cur = cur.next;

}
if (length < k){
return head;
}
prev.next = null;
ListNode left = reverse(head, k);
ListNode right = reverseKGroup(cur, k);
left.next = right;
// prev此时是头结点
return prev;
}

private static ListNode reverse(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}

ListNode prev = head;
int length = 0;
while (prev != null) {
length++;
prev = prev.next;
}
if (length < k){
return head;
}
// 两两翻转
prev = head;
ListNode cur = prev.next;
while (cur != null) {
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
head.next = null;
return head;
}

public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(2);
n1.next = n2;
ListNode n3 = new ListNode(3);
n2.next = n3;
ListNode n4 = new ListNode(4);
n3.next = n4;
ListNode n5 = new ListNode(5);
n4.next = n5;
print(reverseKGroup(n1, 3));

n1 = new ListNode(1);
n2 = new ListNode(2);
n1.next = n2;
n3 = new ListNode(3);
n2.next = n3;
n4 = new ListNode(4);
n3.next = n4;
n5 = new ListNode(5);
n4.next = n5;
print(reverseKGroup(n1, 2));
}

private static void print(ListNode head) {
while (head != null) {
System.out.print(head.val);
head = head.next;
}
System.out.println();
}

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;
}
}
}

打印:

1
2
32145
21435

思路:

这道题解法很简单,先分段反转,然后通过递归拼接。难点在于链表的指针,一不小心就指错了。


ReverseNodesInKGroup
https://yangtzeshore.github.io/2021/01/28/ReverseNodesInKGroup/
作者
Chen Peng
发布于
2021年1月28日
许可协议