LeetCode 25. K 个一组翻转链表
原题链接
简单
作者:
autumn_0
,
2025-01-02 21:26:29
,
所有人可见
,
阅读 5
/**
* 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) {
int n = 0;
for(ListNode cur = head; cur != null; cur = cur.next) n ++ ;
ListNode dummy = new ListNode(0, head);
ListNode pre = null;
ListNode cur = head;
ListNode p0 = dummy;
for( ; n >= k; n -= k){
for(int i = 0; i < k; i ++ ){
ListNode nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
ListNode nxt = p0.next;
p0.next.next = cur;
p0.next = pre;
p0 = nxt;
}
return dummy.next;
}
}