/**
* 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(k == 1||head == NULL)return head;
ListNode* p = head;
ListNode* o = p;
for(int i = 1 ; i < k ; i ++)
{
if(o == NULL)return head;
o = o->next;
}
if(o == NULL)return head;
o->next = reverseKGroup(o->next,k);
o = o->next;
ListNode* q = p->next;
for(int i = 1 ; i < k ; i ++)
{
p -> next = o;
o = p;
p = q;
q = q->next;
}
p -> next = o;
return p;

}
};

posted on 2018-03-24 16:54  朽木の半夏  阅读(78)  评论(0编辑  收藏  举报