-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path92.reverse-linked-list-ii.cpp
More file actions
70 lines (65 loc) · 1.38 KB
/
92.reverse-linked-list-ii.cpp
File metadata and controls
70 lines (65 loc) · 1.38 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
/*
* @lc app=leetcode id=92 lang=cpp
*
* [92] Reverse Linked List II
*
* https://leetcode.com/problems/reverse-linked-list-ii/description/
*
* algorithms
* Medium (32.92%)
* Likes: 1554
* Dislikes: 108
* Total Accepted: 223.3K
* Total Submissions: 612.9K
* 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(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* return_list = nullptr;
ListNode** current = &return_list;
int counter = 1;
while (counter < m) {
*current = head;
head = head->next;
(*current)->next = nullptr;
current = &((*current)->next);
++counter;
}
ListNode** suffix = nullptr;
while (counter <= n) {
ListNode* old_current = *current;
*current = head;
head = head->next;
(*current)->next = old_current;
if (!old_current) {
suffix = &((*current)->next);
}
++counter;
}
*suffix = head;
return return_list;
}
};
// @lc code=end