forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_Linked_List_II.cpp
More file actions
71 lines (63 loc) · 1.46 KB
/
Reverse_Linked_List_II.cpp
File metadata and controls
71 lines (63 loc) · 1.46 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 15, 2012
Problem: Reverse Linked List II
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
Open a new list and traverse the original list, when encounter a node not
in the reverse segment, insert after the tail; else insert before the tail.
This method is O(n).
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
int count = 1;
ListNode *rhead, *cur, *rtail, *newhead;
/* an empty head to make the operation easier */
rhead = new ListNode(1);
newhead = rhead;
rhead->next = head;
while (count < m) {
count++;
rhead = rhead->next;
}
rtail = rhead->next;
cur = rtail->next;
while (count < n) {
ListNode *rnode = cur;
cur = cur->next;
rtail->next = rnode->next;
rnode->next = rhead->next;
rhead->next = rnode;
count++;
}
head = newhead->next;
delete newhead;
return head;
}
};