解题思路:
(1)递归求解
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode *pre=nullptr,*cur=head,*next=head->next;
if(!head || k<1) return head;
for(int i=0;i<k;i++) {
if(cur==nullptr) return head;
cur=cur->next;
}
cur=head;
for(int i=0;i<k;i++) {
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
head->next=reverseKGroup(cur, k);
return pre;
}
};