-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseBetween.h
More file actions
executable file
·41 lines (38 loc) · 1.03 KB
/
reverseBetween.h
File metadata and controls
executable file
·41 lines (38 loc) · 1.03 KB
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
/**
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
*/
/**
* 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) {
// Note: The Solution object is instantiated only once and is reused by each test case.
ListNode dummy(0);
dummy.next = head;
ListNode *preM, *pre = &dummy;
for(int i = 1; i <= n; ++i){
if(i == m) preM = pre;
if(i > m && i <= n){
pre->next = head->next;
head->next = preM->next;
preM->next = head;
head = pre;
}
pre = head;
head = head->next;
}
return dummy.next;
}
};