if(head==NULL || k==1){
return head;
}
ListNode* dummy=new ListNode(0);
dummy->next=head;
ListNode *cur=dummy,*pre=dummy, *nex=dummy;
int cnt=0;
while(cur->next!=NULL){
cur=cur->next;
cnt++;
}
while(cnt>=k){
cur=pre->next;
nex= cur->next;
for(int i=1;i<k;i++){
cout<<dummy->next->val;
cur->next=nex->next;
nex->next=pre->next;
pre->next=nex;
nex=cur->next;
}
pre=cur;
cnt-=k;
}
return dummy->next;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter