Example:
input: 1->2->3->4->5->6->7->8->NULL and k = 3
output:3->2->1->6->5->4->7->8->NULL
Node* inverstList(Node *head, int num) { Node *prev, *curr, *next; int count = 0; if (num == 0 || num == 1) return head; prev = head; curr = prev->next; next = curr->next; while (num-1 > count) { if (curr != NULL) { next = curr->next; curr->next = prev; prev = curr; curr = next; count++; } else { curr = prev->next; prev->next = NULL; while (count > 0) { next = curr->next; curr->next = prev; prev = curr; curr = next; count--; } return prev; } } if (next != NULL) head->next = inverstList(next, num); else head->next = NULL; return prev; }
时间: 2024-10-31 03:48:24