leetCode.92. 反转链表 II

leetCode.92. 反转链表 II


题目思路


代码

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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;
        auto 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;
    }
};
相关推荐
人道领域2 分钟前
【LeetCode刷题日记】:设计链表全解析
算法·leetcode·链表
py有趣9 小时前
力扣热门100题之环形链表
算法·leetcode·链表
py有趣9 小时前
力扣热门100题之回文链表
算法·leetcode·链表
Kk.080210 小时前
数据结构|链表 刷题
数据结构·链表
样例过了就是过了13 小时前
LeetCode热题100 柱状图中最大的矩形
数据结构·c++·算法·leetcode
wsoz13 小时前
Leetcode哈希-day1
算法·leetcode·哈希算法
阿Y加油吧13 小时前
LeetCode 二叉搜索树双神题通关!有序数组转平衡 BST + 验证 BST,小白递归一把梭
java·算法·leetcode
小肝一下15 小时前
每日两道力扣,day2
c++·算法·leetcode·职场和发展
米粒115 小时前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
AlenTech16 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展