struct Node
{
int value;
Node* next;
};
Node* reverse(Node* head, Node* tail)
{
if(head == NULL || head->next == NULL)
return head;
Node* pre = NULL;
Node* next = NULL;
while(pre != tail)
{
next = head->next;
head->next = pre;
pre = head;
head = next;
}
return pre;
}
Node* reverserGroup(Node* head, int k)
{
if(head == NULL || head->next == NULL || k<=1)
return head;
Node* curNode = head;
for(int count = 1; count < k; ++count)
{
curNode = curNode->next;
if(curNode == NULL)
{
return head;
}
}
Node* next = curNode->next;
reverse(head, curNode);
head->next = reverseGroup(next, k);
return curNode;
}