-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path25.cpp
More file actions
executable file
·78 lines (74 loc) · 2.22 KB
/
25.cpp
File metadata and controls
executable file
·78 lines (74 loc) · 2.22 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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
//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) {
ListNode * N = new ListNode(0);
ListNode * Nhead = N;
ListNode * answer = new ListNode(0);
ListNode * answerhead = answer;
ListNode *current_point = head;
int calculated = 0;
while (current_point != NULL) {
calculated ++;
//cout<<current_point->val<<endl;
ListNode * tmp = new ListNode(current_point->val);
N -> next = tmp;
N = N->next;
if (calculated == k){
ListNode * headtmp = Nhead;
while (headtmp -> next != NULL){
ListNode * ttmp = Nhead;
while (ttmp -> next -> next != NULL)
ttmp = ttmp -> next;
answer->next = new ListNode(ttmp ->next->val);
answer = answer->next;
ttmp->next = NULL;
}
N = new ListNode(0);
Nhead = N;
calculated = 0;
}
if (current_point -> next == NULL) {
ListNode * headtmp = Nhead;
headtmp = headtmp->next;
while (headtmp != NULL){
ListNode * anstmp = new ListNode(headtmp->val);
answer->next = anstmp;
answer = answer->next;
headtmp = headtmp->next;
}
break;
}
current_point = current_point->next;
}
return answerhead->next;
}
};
int main(){
ListNode * test = new ListNode(1);
ListNode * header = test;
ListNode * tmp = new ListNode(2);
test->next = tmp;
test =test->next;
tmp = new ListNode(3);
test->next = tmp;
test =test->next;
tmp = new ListNode(4);
test->next = tmp;
test =test->next;
tmp = new ListNode(5);
test->next = tmp;
test =test->next;
Solution s;
s.reverseKGroup(header,2);
}