首页 > 其他 > 详细

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

时间:2020-07-16 23:10:10      阅读:61      评论:0      收藏:0      [点我收藏+]

代码

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

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

原文:https://www.cnblogs.com/clown9804/p/13325137.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!