-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulate_n.cpp
47 lines (38 loc) · 1.01 KB
/
AC_simulate_n.cpp
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulate_n.cpp
* Create Date: 2015-01-07 09:50:29
* Descripton: Use two points, one points to the (m-1)-th,
* another points to the (m+i)th
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
// 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) {
ListNode *mhead = new ListNode(0), *prev, *cur;
mhead->next = head; // because m will be 0
for (int i = 0; i < m - 1; i++) {
mhead = mhead->next;
}
prev = mhead->next;
cur = prev->next;
for (int i = m; i < n; i++) {
prev->next = cur->next;
cur->next = mhead->next;
mhead->next = cur;
cur = prev->next;
}
return m == 1 ? mhead->next : head;
}
};
int main() {
return 0;
}