File tree Expand file tree Collapse file tree 1 file changed +19
-20
lines changed
Linked_List/092.Reverse-Linked-List-II Expand file tree Collapse file tree 1 file changed +19
-20
lines changed Original file line number Diff line number Diff line change @@ -10,30 +10,29 @@ class Solution {
10
10
public:
11
11
ListNode* reverseBetween (ListNode* head, int m, int n)
12
12
{
13
- ListNode* dummyHead = new ListNode (0 );
14
- dummyHead ->next = head;
15
-
16
- ListNode* p=dummyHead;
13
+ ListNode* dummy = new ListNode (0 );
14
+ dummy ->next = head;
15
+ ListNode* cur = dummy;
16
+
17
17
for (int i=1 ; i<=m-1 ; i++)
18
- p=p->next ;
19
- ListNode* end_of_first=p;
20
- p=p->next ;
21
-
22
- ListNode* last=p;
23
- ListNode* front = NULL ;
18
+ cur = cur->next ;
19
+
20
+ ListNode* end_of_fisrt = cur;
21
+ cur = cur->next ;
22
+
23
+ ListNode* start_of_second = cur;
24
+ ListNode* prev = NULL ;
24
25
for (int i=m; i<=n; i++)
25
26
{
26
- ListNode* temp = p ->next ;
27
- p ->next =front ;
28
- front=p ;
29
- p=temp ;
27
+ ListNode* next = cur ->next ;
28
+ cur ->next = prev ;
29
+ prev = cur ;
30
+ cur = next ;
30
31
}
31
-
32
- end_of_first->next =front;
33
- last->next = p;
34
-
35
- return dummyHead->next ;
36
-
37
32
33
+ end_of_fisrt->next = prev;
34
+ start_of_second->next = cur;
35
+ return dummy->next ;
38
36
}
37
+
39
38
};
You can’t perform that action at this time.
0 commit comments