-
Notifications
You must be signed in to change notification settings - Fork 11
/
25.k-个一组翻转链表.java
48 lines (44 loc) · 1.2 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
/*
* @lc app=leetcode.cn id=25 lang=java
*
* [25] K 个一组翻转链表
*/
// @lc code=start
/**
* 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 Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null) return null;
ListNode a,b;
a = b = head;
for(int i = 0;i<k;i++){
// 这里实现了 如果节点总数不是K的整数倍,那么请将最后剩余的节点保持原有顺序
if(b == null) return head;
b = b.next;
}
ListNode newHead = reverse(a,b);
a.next = reverseKGroup(b,k);
return newHead;
}
ListNode reverse(ListNode a,ListNode b){
ListNode pre = null ;
ListNode cur = a;
ListNode nxt = a;
while(cur != b){
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
}
// @lc code=end