-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse-nodes-in-k-group.cpp
63 lines (60 loc) · 1.83 KB
/
reverse-nodes-in-k-group.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 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) {
if(head==NULL || k==0 || k==1) return head;
ListNode* root=head,*pre=NULL;
int len=0,count=0,time=0;
while(head!=NULL){
len+=1;
head=head->next;
}
// cout<<len<<" "<<k<<endl;
if(len<k) return root;
ListNode** ListArray=new ListNode* [len];
head=root;
while(count<len)
{
count+=k;
time=0;
for(int i=0;i<k;i++){
if(head!=NULL){
// cout<<head->val<<endl;
ListArray[i]=head;
head=head->next;
time+=1;
}
}
// if(head!=NULL) head=head->next;
// cout<<ListArray[k-1]->val<<endl;
if(pre==NULL) root=reversePart(ListArray,time,k);
else
pre->next=reversePart(ListArray,time,k);
pre=ListArray[0];
// cout<<"E "<<ListArray[0]->next->val<<endl;
}
if(time==k) pre->next=NULL;
ListNode* tmp=root;
while(tmp!=NULL){
// cout<<tmp->val<<" "<<endl;
tmp=tmp->next;
}
return root;
}
ListNode* reversePart(ListNode* ListArray[],int time,int k){
if(time==0) return NULL;
if(time!=k) return ListArray[0];
for(int i=k-1;i>=1;i--){
ListArray[i]->next=ListArray[i-1];
}
return ListArray[k-1];
}
};