更新时间:2018-11-22 15:22作者:李一老师
}
void printLink(node *head)
{
while (head->next != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("%d ", head->data);
}
int linkLen(node *head)
{
int len = 0;
while (head != NULL) {
len ++;
head = head->next;
}
return len;
}
node* reverseK(node *head, int k)
{
int i, len, time, now;
len = linkLen(head);
if (len < k) {
return head;
} else {
time = len / k;
}
node *newhead, *prev, *next, *old, *tail;
for (now = 0, tail = NULL; now < time; now ++) {
old = head;
for (i = 0, prev = NULL; i < k; i ++) {
next = head->next;
head->next = prev;
prev = head;
head = next;
}
if (now == 0) {
newhead = prev;
}
old->next = head;
if (tail != NULL) {
tail->next = prev;