-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathReverseNodesInKGroup.java
More file actions
117 lines (108 loc) · 3.4 KB
/
Copy pathReverseNodesInKGroup.java
File metadata and controls
117 lines (108 loc) · 3.4 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
/**
* <p>给你一个链表,每 <em>k </em>个节点一组进行翻转,请你返回翻转后的链表。</p>
*
* <p><em>k </em>是一个正整数,它的值小于或等于链表的长度。</p>
*
* <p>如果节点总数不是 <em>k </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p>
*
* <p><strong>进阶:</strong></p>
*
* <ul>
* <li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li>
* <li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li>
* </ul>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg" style="width: 542px; height: 222px;" />
* <pre>
* <strong>输入:</strong>head = [1,2,3,4,5], k = 2
* <strong>输出:</strong>[2,1,4,3,5]
* </pre>
*
* <p><strong>示例 2:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" style="width: 542px; height: 222px;" />
* <pre>
* <strong>输入:</strong>head = [1,2,3,4,5], k = 3
* <strong>输出:</strong>[3,2,1,4,5]
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>head = [1,2,3,4,5], k = 1
* <strong>输出:</strong>[1,2,3,4,5]
* </pre>
*
* <p><strong>示例 4:</strong></p>
*
* <pre>
* <strong>输入:</strong>head = [1], k = 1
* <strong>输出:</strong>[1]
* </pre>
*
* <ul>
* </ul>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li>列表中节点的数量在范围 <code>sz</code> 内</li>
* <li><code>1 <= sz <= 5000</code></li>
* <li><code>0 <= Node.val <= 1000</code></li>
* <li><code>1 <= k <= sz</code></li>
* </ul>
* <div><div>Related Topics</div><div><li>递归</li><li>链表</li></div></div><br><div><li>👍 1443</li><li>👎 0</li></div>
*/
package leetcode1;
public class ReverseNodesInKGroup {
public static void main(String[] args) {
Solution solution = new ReverseNodesInKGroup().new Solution();
}
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
/**
* 参考题解,画图看一看就懂了,挺简单
* https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode prev = new ListNode();
prev.next = head;
head = prev;
while (true) {
ListNode end = prev;
for (int i = 0; i < k && end != null; i++) end = end.next;
if (end == null) return head.next;
ListNode start = prev.next;
ListNode next = end.next;
end.next = null;
prev.next = reverse(start);
start.next = next;
prev = start;
}
}
private ListNode reverse(ListNode node) {
ListNode prev = null;
while (node != null) {
ListNode next = node.next;
node.next = prev;
prev = node;
node = next;
}
return prev;
}
}
}