/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if(head==NULL||head->next==NULL||k<2)
return head;
ListNode *nexthead=head;
for(int i=0;i<k;i++)
{
if(nexthead)
{
nexthead=nexthead->next;
}
else return head;
}
ListNode *newhead=reverseKGroup(nexthead,k);
ListNode *pre=NULL;
ListNode *curr=head;
while(curr!=nexthead)//在这错了好几次了
{
ListNode *temp=curr->next;
curr->next=pre?pre:newhead;
pre=curr;
curr=temp;
}
return pre;
}
};
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if(head==NULL||head->next==NULL||k<2)
return head;
ListNode *nexthead=head;
for(int i=0;i<k;i++)
{
if(nexthead)
{
nexthead=nexthead->next;
}
else return head;
}
ListNode *newhead=reverseKGroup(nexthead,k);
ListNode *pre=NULL;
ListNode *curr=head;
while(curr!=nexthead)//在这错了好几次了
{
ListNode *temp=curr->next;
curr->next=pre?pre:newhead;
pre=curr;
curr=temp;
}
return pre;
}
};