单链表反转
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* next;
while(cur) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
K之前链表反转
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* next=NULL;
for(int i = 0; i < k; i++) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head->next = cur;
return pre;
}