-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.cpp
38 lines (37 loc) · 799 Bytes
/
25.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// 25.cpp
// leetcode
//
// Created by R Z on 2018/2/27.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
/**
* 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) {
ListNode* cur = head;
int count = 0;
while(cur!=NULL && count!=k){
cur=cur->next;
count++;
}
if(count==k){
cur=reverseKGroup(cur,k);
while(count--){
ListNode* tmp = head->next;
head->next=cur;
cur=head;
head=tmp;
}
head=cur;
}
return head;
}
};