-
Notifications
You must be signed in to change notification settings - Fork 0
/
0092.反转链表-ii.cpp
63 lines (59 loc) · 1.29 KB
/
0092.反转链表-ii.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
/*
* @lc app=leetcode.cn id=92 lang=cpp
*
* [92] 反转链表 II
*
* https://leetcode-cn.com/problems/reverse-linked-list-ii/description/
*
* algorithms
* Medium (46.26%)
* Likes: 263
* Dislikes: 0
* Total Accepted: 27.8K
* Total Submissions: 57.7K
* Testcase Example: '[1,2,3,4,5]\n2\n4'
*
* 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
*
* 说明:
* 1 ≤ m ≤ n ≤ 链表长度。
*
* 示例:
*
* 输入: 1->2->3->4->5->NULL, m = 2, n = 4
* 输出: 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)
{
if (m == 1) {
return reverseN(head, n);
}
head->next = reverseBetween(head->next, m - 1, n - 1);
return head;
}
ListNode* successor = nullptr;
ListNode* reverseN(ListNode* head, int n)
{
if (n == 1) {
successor = head->next;
return head;
}
ListNode* last = reverseN(head->next, n - 1);
head->next->next = head;
head->next = successor;
return last;
}
};
// @lc code=end