Neo's Blog

不抽象就无法深入思考
不还原就看不到本来面目!

0%

链表反转系列-m到n链表反转

逆序第m~n个元素

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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* successor;

//非递归实现
ListNode* reverseN(ListNode* head, int n) {
ListNode* pre = NULL;
auto cur = head;
while (cur && n > 0) {
auto next = cur->next; //记录下一个cur
cur->next = pre; //核心步骤---操作cur的next指针,让其指向pre;
pre = cur; //记录前一个
cur = next; //给下一个cur赋值
n--;
}

head->next = cur;
return pre;
}

ListNode* reverseN(ListNode* head, int n) {
if (!head) return head;
//base
if (n == 1) {
successor = head->next; //这一步特别关键,记录当前元素的后继节点,则外面会使用。
return head;
}

//子问题-下一个节点
auto x = reverseN(head->next, n - 1);
//head->next 是递归之前的头,也就是递归之后的尾巴
(head->next)->next = head;
head->next = successor;
return x;
}

ListNode* reverseBetween(ListNode* head, int m, int n) {
//base
if (m == 1) {
return reverseN(head, n);
}

auto x = reverseBetween(head->next, m - 1, n - 1);
head->next = x;
return head; //返回的是头
}
};

你的支持是我坚持的最大动力!