-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathreverseKGroup.cpp
More file actions
32 lines (32 loc) · 1 KB
/
Copy pathreverseKGroup.cpp
File metadata and controls
32 lines (32 loc) · 1 KB
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
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head==NULL || k==1){
return head;
}
ListNode* dummy=new ListNode(0);
dummy->next=head;
ListNode* current=dummy;
ListNode* Next=dummy;
ListNode* prev=dummy;
int count=0;
while(current->next!=NULL){
current=current->next;
count++;
}
cout<<count<<" ";
while(count>=k){
current=prev->next;
Next=current->next;
for(int i=1;i<k;i++){
current->next=Next->next;
Next->next=prev->next;
prev->next=Next;
Next=current->next;
}
prev=current;
count=count-k;
}
return dummy->next;
}
};