ListNode *newTail = new ListNode(0);//new a fake head node
ListNode *newPtr = newTail;
ListNode *ptr = head;
ListNode *next;
while (ptr != NULL)
{
next = ptr;
int len = k;
while (len > 0 && next != NULL)//find the scope of k
{
next = next->next;
len--;
}
if (len == 0) //this start reverse K-Scope
{
while (ptr != next)
{
ListNode *tmp = ptr;
ptr = ptr->next;
tmp->next = newPtr->next;
newPtr->next = tmp;
}
}
else //put the last ListNode connect it
{
newPtr->next = ptr;
break;
}
while (newPtr->next != NULL) //start a new process
{
newPtr = newPtr->next;
}
}
return newTail->next;
【LeetCode】Reverse Nodes in k-Group
最新推荐文章于 2015-03-05 11:49:30 发布