穿针引线
以此类推
ListNode* reverseBetween(ListNode* head, int left, int right) { ListNode* dummyNode = new ListNode(-1); dummyNode->next = head; ListNode *pre = dummyNode; for (int i = 0; i < left-1; ++i) { pre = pre->next; } ListNode* cur = pre->next; ListNode* next; for (int i = 0; i < right-left; ++i) { next = cur->next; cur->next = next->next; //从这开始 next->next = pre->next; pre->next = next; } return dummyNode->next; }
版权声明:《 leetcode 反转链表 II 》为admin原创文章,转载请注明出处!
最后编辑:2021-3-18 11:03:15