File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments