/
92.ReverseLinkedListIi.cpp
74 lines (70 loc) · 1.71 KB
/
92.ReverseLinkedListIi.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
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
72
73
74
/*
* @lc app=leetcode id=92 lang=cpp
*
* [92] Reverse Linked List II
*
* https://leetcode.com/problems/reverse-linked-list-ii/description/
*
* algorithms
* Medium (40.49%)
* Likes: 3321
* Dislikes: 169
* Total Accepted: 328.4K
* Total Submissions: 811K
* Testcase Example: '[1,2,3,4,5]\n2\n4'
*
* Reverse a linked list from position m to n. Do it in one-pass.
*
* Note: 1 ≤ m ≤ n ≤ length of list.
*
* Example:
*
*
* Input: 1->2->3->4->5->NULL, m = 2, n = 4
* Output: 1->4->3->2->5->NULL
*
*
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* dummy = new ListNode();
dummy->next = head;
ListNode *first_tail, *second_tail;
ListNode *curr = dummy, *prev, *next;
int step = 1;
// move curr to the node just before the mth node, mark it as tail for
// the first part and mth node should be tail for second part
while (step < m) {
curr = curr->next;
step++;
}
first_tail = curr;
curr = curr->next;
second_tail = curr;
prev = curr;
curr = curr->next;
while (step < n) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
step++;
}
first_tail->next = prev;
second_tail->next = curr;
return dummy->next;
}
};
// @lc code=end