-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path92.cpp
More file actions
26 lines (26 loc) · 699 Bytes
/
Copy path92.cpp
File metadata and controls
26 lines (26 loc) · 699 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
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
if (m >= n || m < 1)
return head;
ListNode *start = new ListNode(0), *prev = start, *tempHead = start;
start->next = head;
int k = n - m;
while (m > 1) {
prev = head;
head = head->next;
--m;
}
ListNode *temp = head->next, *link = head, *next = nullptr;
while (k > 0) {
next = temp->next;
temp->next = head;
head = temp;
temp = next;
--k;
}
prev->next = head;
link->next = next;
return tempHead->next;
}
};