***25 k个一组反转链表
Posted INnoVation-V2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了***25 k个一组反转链表相关的知识,希望对你有一定的参考价值。
ListNode *reverseKGroup(ListNode *head, int k) {
if (head == nullptr || head->next == nullptr || k == 1)
return head;
int num = 0;
ListNode *preheader = new ListNode(-1);
preheader->next = head;
ListNode *cur = preheader, *nex, *pre = preheader;
while ((cur = cur->next))
num++;
while (num >= k) {
cur = pre->next;
nex = cur->next;
for (int i = 1; i < k; ++i) {
cur->next = nex->next;
nex->next = pre->next;
pre->next = nex;
nex = cur->next;
}
pre = cur;
num -= k;
}
return preheader->next;
}
以上是关于***25 k个一组反转链表的主要内容,如果未能解决你的问题,请参考以下文章