forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0092.cpp
More file actions
41 lines (34 loc) · 837 Bytes
/
0092.cpp
File metadata and controls
41 lines (34 loc) · 837 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (!head) return NULL;
ListNode* prev = NULL;
ListNode* curr = head;
for (int i = 0; i < m - 1; i++) {
prev = curr;
curr = curr->next;
}
ListNode* conn = prev;
ListNode* tail = curr;
for (int i = 0; i < n - m + 1; i++) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
if (conn)
conn->next = prev;
else
head = prev;
tail->next = curr;
return head;
}
};