标签:cpp next list || for ever ptr ade eve
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;
}
标签:cpp next list || for ever ptr ade eve
原文地址:https://www.cnblogs.com/INnoVationv2/p/10175479.html