leetcode 反转链表 II

穿针引线

以此类推

    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;
    }



发表评论 / Comment

用心评论~