- 基本思想:
主要分为两部分,找到reorder的前一个节点 + 进行reorder
C++:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* aa = new ListNode(-1);
aa->next = head;
ListNode* tt = aa;
for (int i = 0; i < m - 1; i++) {
tt = tt->next;
}
ListNode* start = tt->next;
ListNode* end = tt->next;
ListNode* t2 = start->next;
for (int i = 0; i < n-m; i++) {
ListNode* t3 = t2->next;
t2->next = start;
start = t2;
t2 = t3;
}
tt->next = start;
end->next = t2;
return aa->next;
}
- 注意:
reverse只能用标准三个指针用法,之前的智能一指针用法只支持全部链表reverse.
网友评论