-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path92.cpp
More file actions
45 lines (43 loc) · 953 Bytes
/
Copy path92.cpp
File metadata and controls
45 lines (43 loc) · 953 Bytes
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
#include "common.h"
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
auto* t = new ListNode(0, head);
auto* new_head = t;
for (int i = 1; i < left; ++i) {
t = t->next;
}
int n = right - left + 1;
auto* pre = t->next;
auto* next = t->next;
while (n--) {
auto* temp = next->next; // 3
next->next = t->next;
t->next = next;
pre->next = temp;
next = temp;
}
return new_head->next;
}
};
int main() {
Solution s;
ListNode* head = new ListNode();
auto* t = head;
/*for (auto i = 1; i <= 2; ++i) {
t->next = new ListNode();
t = t->next;
t->val = i;
t->next = nullptr;
}
PrintList(head->next);*/
for (auto i = 1; i <= 5; ++i) {
t->next = new ListNode();
t = t->next;
t->val = i;
t->next = nullptr;
}
PrintList(head->next);
head = s.reverseBetween(head->next, 2, 4);
PrintList(head);
}