-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#73.cc
More file actions
50 lines (48 loc) · 1.2 KB
/
LeetCode#73.cc
File metadata and controls
50 lines (48 loc) · 1.2 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
42
43
44
45
46
47
48
49
50
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(m==n) return head;
ListNode * pre = NULL;
ListNode *cur = head;
ListNode *pre1;
ListNode *left;
int cnt = 0;
while(cur){
cnt++;
ListNode * next = cur->next;
if(cnt == m){
pre1 = pre;
left = cur;
pre = cur;
cur = next;
}
else if(cnt > m && cnt <n){
cur->next = pre;
pre = cur;
cur = next;
}
else if(cnt == n){
cur->next = pre;
left->next = next;
if(pre1) pre1->next=cur;
else head = cur;
cur = next;
}
else{
pre = cur;
cur = next;
}
}
return head;
}
};