class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k)
{
if(!head)
return nullptr;
ListNode* cur=head;
int count=0;
while(cur)
{
++count;
cur=cur->next;//遍历有几个节点;
}
int times=count/k;//需要进行反转的组数;
ListNode* dummyHead=new ListNode(0);//头节点
dummyHead->next=head;//连接;
stack<ListNode*> s;//创建栈
cur=dummyHead;//回调cur指针
ListNode* temp;//创建桥梁指针;
for(int i=0;i<times;++i)
{
temp=cur->next;//即题目给的头节点
for(int j=0;j<k;++j)//k个数
{
s.push(temp);
temp=temp->next;//让temp指向压完栈的后一个节点配合下面
}
for(int j=0;j<k;++j)
{
cur->next=s.top();
s.pop();
cur=cur->next;
}
cur->next=temp;//配合上面压完栈的操作
}
return dummyHead->next;
}
};