LeetCode 25 K个一组翻转链表
Posted Starzkg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 25 K个一组翻转链表相关的知识,希望对你有一定的参考价值。
https://leetcode-cn.com/problems/reverse-nodes-in-k-group/
解决方案
class Solution
public ListNode reverseKGroup(ListNode head, int k)
return reverseKGroup(head, head, k, 1);
public ListNode reverseKGroup(ListNode head, ListNode tail, int k, int k0)
if (tail == null)
return head;
if (k0 % k == 0)
ListNode nex = reverseKGroup(tail.next, tail.next, k, k0 + 1);
ListNode prev = head;
ListNode now = head.next;
while (prev != tail)
ListNode next = now.next;
now.next = prev;
prev = now;
now = next;
head.next = nex;
return tail;
return reverseKGroup(head, tail.next, k, k0 + 1);
以上是关于LeetCode 25 K个一组翻转链表的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 25. K 个一组翻转链表 | Python