leetcode-----92. 反转链表 II

代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        auto dummy = new ListNode(-1);
        dummy->next = head;

        auto a  = dummy;
        for (int i = 0; i < m - 1; ++i) a = a->next;
        auto b = a->next, c = b->next;
        for (int i = 0; i < n - m; ++i) {
            auto t = c->next;
            c->next = b;
            b = c, c = t;
        }
        a->next->next = c;
        a->next = b;
        return dummy->next;
    }
};
posted @ 2020-07-16 21:14  景云ⁿ  阅读(51)  评论(0编辑  收藏  举报