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;
    }
};
相关推荐
一梦浮华15 分钟前
自学嵌入式 day20-数据结构 链表
数据结构·链表
alphaTao42 分钟前
LeetCode 每日一题 2025/5/12-2025/5/18
算法·leetcode
exe4522 小时前
力扣每日一题5-18
java·算法·leetcode
阳洞洞3 小时前
leetcode 74. Search a 2D Matrix
leetcode·二分查找
YuforiaCode3 小时前
LeetCode 219.存在重复元素 II
算法·leetcode·职场和发展
小雅痞3 小时前
[Java][Leetcode middle] 151. 反转字符串中的单词
java·leetcode
飞川撸码13 小时前
【LeetCode 热题100】739:每日温度(详细解析)(Go语言版)
算法·leetcode·golang
小学生的信奥之路16 小时前
力扣1991:找到数组的中间位置(前缀和)
数据结构·算法·leetcode·前缀和·数组
এ᭄画画的北北16 小时前
力扣-102.二叉树的层序遍历
数据结构·算法·leetcode
JeffersonZU18 小时前
【数据结构】2-3-1单链表的定义
数据结构·链表