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;
    }
};
相关推荐
AKA__Zas1 小时前
芝士算法(位运算)
java·开发语言·算法·leetcode·学习方法
玖玥拾18 小时前
LeetCode 88 合并两个有序数组
算法·leetcode
Hi李耶19 小时前
【LeetCode】541.反转字符串 II
算法·leetcode·职场和发展
白白白小纯1 天前
每日算法day3—回文链表,链表分割
c语言·数据结构·算法·leetcode
zander2581 天前
35. 搜索插入位置:从边界语义理解二分查找
数据结构·算法·leetcode
yyds_yyd_100861 天前
3731. 找出缺失的元素(2026.08.04)
c++·leetcode
lueluelue471 天前
LeetCode:链表
算法·leetcode·链表
橘子汽水1682 天前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表
Re.不晚2 天前
挑战做100道力扣算法- DAY1
算法·leetcode·职场和发展
青山木2 天前
Hot 100 --- 搜索插入位置
java·数据结构·算法·leetcode