Skip to content

Commit 26321d0

Browse files
committed
2
1 parent 29b9388 commit 26321d0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *reverseBetween(ListNode *head, int m, int n) {
12+
// Start typing your C/C++ solution below
13+
// DO NOT write int main() function
14+
15+
ListNode prev_head(0);
16+
prev_head.next = head;
17+
ListNode *prev = &prev_head;
18+
ListNode *current = head;
19+
20+
for (int i = 0; i < m - 1; i++) {
21+
prev = prev->next;
22+
current = current->next;
23+
}
24+
ListNode *end = current;
25+
for (int i = m - 1; i < n; i++) {
26+
ListNode *next = current->next;
27+
current->next = prev->next;
28+
prev->next = current;
29+
current = next;
30+
}
31+
end->next = current;
32+
return prev_head.next;
33+
}
34+
};

0 commit comments

Comments
 (0)