-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.k-个一组翻转链表.java
130 lines (121 loc) · 3.74 KB
/
25.k-个一组翻转链表.java
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
118
119
120
121
122
123
124
125
126
127
128
129
/*
* @lc app=leetcode.cn id=25 lang=java
*
* [25] K 个一组翻转链表
*/
// @lc code=start
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Definition for singly-linked list.
* 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; }
* }
*/
// class ListNode {
// int val;
// ListNode next;
// ListNode() {}
// ListNode(int val) { this.val = val; }
// ListNode(int val, ListNode next) { this.val = val; this.next = next; }
// }
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// return reverseKGroupTwoPointers(head, k);
// return reverseKGroupStack(head, k);
return reverseKGroupRecursion(head, k);
}
public ListNode reverseKGroupTwoPointers(ListNode head, int k) {
// this problem is a combination of "linked list reversion" and
// "linked list iteraion"
ListNode dummy = new ListNode(0, head);
// preHead will be updated iteratively
ListNode preHead = dummy;
ListNode node = head;
while (node != null) {
int i = 0;
for (i = 0; i < k && node != null; i++) {
node = node.next;
}
// now, nodes between preHead(ex) and node(ex) should be reversed
if (i == k) {
preHead = reverse(preHead, k);
preHead.next = node;
} else {
break;
}
}
return dummy.next;
}
ListNode reverse(ListNode preHead, int k) {
// the caller will make sure preHead have more than k suffix
// return the tail node
ListNode pre = preHead;
ListNode node = preHead.next;
ListNode tail = node;
for (int i = 0; i < k; i++) {
ListNode next = node.next;
node.next = pre;
pre = node;
node = next;
}
preHead.next = pre;
return tail;
}
public ListNode reverseKGroupStack(ListNode head, int k) {
// this problem is a combination of "linked list reversion" and
// "linked list iteraion"
ListNode dummy = new ListNode(0, head);
// preHead will be updated iteratively
ListNode preHead = dummy;
ListNode node = head;
Deque<ListNode> stack = new ArrayDeque<>();
while (node != null) {
int i = 0;
for (i = 0; i < k && node != null; i++) {
stack.push(node);
node = node.next;
}
// now, nodes between preHead(ex) and node(ex) should be reversed
if (i == k) {
for (i = 0; i < k; i++) {
preHead.next = stack.pop();
preHead = preHead.next;
}
preHead.next = node;
} else {
break;
}
}
return dummy.next;
}
public ListNode reverseKGroupRecursion(ListNode head, int k) {
ListNode node = head;
int i;
for (i = 0; i < k && node != null; i++) {
node = node.next;
}
if (i == k) {
ListNode nextHead = node;
ListNode pre = null;
node = head;
// linked list reversion
for (i = 0; i < k; i++) {
ListNode next = node.next;
node.next = pre;
pre = node;
node = next;
}
// now, head is the tail of this segment
head.next = reverseKGroupRecursion(nextHead, k);
return pre;
} else {
return head;
}
}
}
// @lc code=end