题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
#include <iostream> using namespace std; class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode* pre, *cur, *dummy; dummy = new ListNode(0); dummy->next = head; pre = dummy; cur = head; for (int i = 1; i < m; i++) { pre = pre->next; cur = cur->next; }// pre , cur for (int i = m; i < n; i++) { ListNode* tmp = cur->next; cur->next = tmp->next; //因为tmp要往前抽,所以cur->next 跳过tmp 变成tmp->next tmp->next = pre->next; //这里pre是定死的,所以每次都是把后面的往前抽,直到cur->next 是最后一个元素,也就是i=n-1时 pre->next = tmp; //把tmp抽到最前面 } return dummy->next; } };#我的实习求职记录#