网站首页 区块链学习

leetcode 反转链表 II

发布时间:2021-3-18 11:31 Thursday编辑:admin阅读(317)

    穿针引线

    以此类推

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