Skip to content

Commit 50a8e76

Browse files
committed
feat: add 025
1 parent 827e520 commit 50a8e76

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
|4|[Median of Two Sorted Arrays][004]|Array, Binary Search, Divide and Conquer|
7474
|10|[Regular Expression Matching][010]|String, Dynamic Programmin, Backtracking|
7575
|23|[Merge k Sorted Lists][023]|Linked List, Divide and Conquer, Heap|
76+
|25|[Reverse Nodes in k-Group][025]|Linked List|
7677

7778

7879

@@ -126,3 +127,4 @@
126127
[004]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md
127128
[010]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/010/README.md
128129
[023]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/023/README.md
130+
[025]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/025/README.md

note/025/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [Merge k Sorted Lists][title]
2+
3+
## Description
4+
5+
Given a linked list, reverse the nodes of a linked list *k* at a time and return its modified list.
6+
7+
*k* is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of *k* then left-out nodes in the end should remain as it is.
8+
9+
You may not alter the values in the nodes, only nodes itself may be changed.
10+
11+
Only constant memory is allowed.
12+
13+
For example,
14+
Given this linked list: `1->2->3->4->5`
15+
16+
For *k* = 2, you should return: `2->1->4->3->5`
17+
18+
For *k* = 3, you should return: `3->2->1->4->5`
19+
20+
**Tags:** Linked List
21+
22+
23+
## 思路
24+
25+
题意是让你以`k`个元素为一组来翻转链表,最后不足`k`个的话不需要翻转,最传统的思路就是每遇到`k`个元素,对其`k`个元素进行翻转,而难点就是在此,下面介绍其原理,我们以例子中的`k = 3`为例,其`pre``next`如下所示。
26+
```
27+
0->1->2->3->4->5
28+
| |
29+
pre next
30+
```
31+
我们要做的就是把`pre``next`之间的元素逆序,思想是依次从第二个元素到第`k`个元素,依次把它插入到`pre`后面,过程如下。
32+
```
33+
head move
34+
| |
35+
0->1->2->3->4->5
36+
| |
37+
pre next
38+
39+
head move
40+
| |
41+
0->2->1->3->4->5
42+
| |
43+
pre next
44+
45+
head move
46+
| |
47+
0->3->2->1->4->5
48+
| |
49+
pre next
50+
```
51+
好了,根据原理,那写出代码就不难了。
52+
53+
54+
```java
55+
/**
56+
* Definition for singly-linked list.
57+
* public class ListNode {
58+
* int val;
59+
* ListNode next;
60+
* ListNode(int x) { val = x; }
61+
* }
62+
*/
63+
class Solution {
64+
public ListNode reverseKGroup(ListNode head, int k) {
65+
if (head == null || k == 1) return head;
66+
ListNode node = new ListNode(0), pre = node;
67+
node.next = head;
68+
for (int i = 1; head != null; ++i) {
69+
if (i % k == 0) {
70+
pre = reverse(pre, head.next);
71+
head = pre.next;
72+
} else {
73+
head = head.next;
74+
}
75+
}
76+
return node.next;
77+
}
78+
79+
private ListNode reverse(ListNode pre, ListNode next) {
80+
ListNode head = pre.next;
81+
ListNode move = head.next;
82+
while (move != next) {
83+
head.next = move.next;
84+
move.next = pre.next;
85+
pre.next = move;
86+
move = head.next;
87+
}
88+
return head;
89+
}
90+
}
91+
```
92+
93+
94+
## 结语
95+
96+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
97+
98+
99+
100+
[title]: https://leetcode.com/problems/merge-k-sorted-lists
101+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.blankj.hard._025;
2+
3+
import com.blankj.structure.ListNode;
4+
5+
/**
6+
* <pre>
7+
* author: Blankj
8+
* blog : http://blankj.com
9+
* time : 2017/10/16
10+
* desc :
11+
* </pre>
12+
*/
13+
public class Solution {
14+
public ListNode reverseKGroup(ListNode head, int k) {
15+
if (head == null || k == 1) return head;
16+
ListNode node = new ListNode(0), pre = node;
17+
node.next = head;
18+
for (int i = 1; head != null; ++i) {
19+
if (i % k == 0) {
20+
pre = reverse(pre, head.next);
21+
head = pre.next;
22+
} else {
23+
head = head.next;
24+
}
25+
}
26+
return node.next;
27+
}
28+
29+
private ListNode reverse(ListNode pre, ListNode next) {
30+
ListNode head = pre.next;
31+
ListNode move = head.next;
32+
while (move != next) {
33+
head.next = move.next;
34+
move.next = pre.next;
35+
pre.next = move;
36+
move = head.next;
37+
}
38+
return head;
39+
}
40+
41+
public static void main(String[] args) {
42+
Solution solution = new Solution();
43+
ListNode.print(solution.reverseKGroup(ListNode.createTestData("[1,2,3,4,5,6,7,8]"), 3));
44+
}
45+
}

0 commit comments

Comments
 (0)