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;
    }
};
相关推荐
熬了夜的程序员19 小时前
【LeetCode】89. 格雷编码
算法·leetcode·链表·职场和发展·矩阵
dragoooon3421 小时前
[优选算法专题四.前缀和——NO.31~32 连续数组、矩阵区域和]
数据结构·算法·leetcode·1024程序员节
熬了夜的程序员1 天前
【LeetCode】87. 扰乱字符串
算法·leetcode·职场和发展·排序算法
·白小白1 天前
力扣(LeetCode) ——15.三数之和(C++)
c++·算法·leetcode
海琴烟Sunshine1 天前
leetcode 268. 丢失的数字 python
python·算法·leetcode
仰泳的熊猫1 天前
LeetCode:268. 丢失的数字
数据结构·c++·算法·leetcode
VT.馒头1 天前
【力扣】2725. 间隔取消
javascript·leetcode·1024程序员节
yy_xzz1 天前
【数据结构】大话单链表
数据结构·链表
咪咪渝粮1 天前
108. 将有序数组转换为二叉搜索树
算法·leetcode
苏纪云1 天前
算法<C++>——双指针操作链表
c++·算法·链表·双指针