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热题100--152. 乘积最大子数组--中等
算法·leetcode·职场和发展
梭七y2 小时前
【力扣hot100题】(103)移动零
数据结构·算法·leetcode
Jeremy爱编码3 小时前
leetcode热题腐烂的橘子
算法·leetcode·职场和发展
alphaTao3 小时前
LeetCode 每日一题 2025/12/22-2025/12/28
算法·leetcode
小白菜又菜4 小时前
Leetcode 1523. Count Odd Numbers in an Interval Range
算法·leetcode
小白菜又菜5 小时前
Leetcode 944. Delete Columns to Make Sorted
算法·leetcode
橘颂TA7 小时前
【剑斩OFFER】算法的暴力美学——链表相加(二)
数据结构·链表·牛客·结构与算法
Swift社区7 小时前
LeetCode 458 - 可怜的小猪
算法·leetcode·职场和发展
Dream it possible!8 小时前
LeetCode 面试经典 150_分治_将有序数组转换为二叉搜索树(105_108_C++_简单)(递归)
c++·leetcode·面试
Q741_1478 小时前
C++ 栈 模拟 力扣 227. 基本计算器 II 题解 每日一题
c++·算法·leetcode·模拟