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;
    }
};
相关推荐
Xin7701 天前
LeetCode 23.合并 K 个升序链表(分治递归)
leetcode
Nil2081 天前
leetcode 105从前序和中序遍历序列构造二叉树
算法·leetcode·职场和发展
Nil2081 天前
leetcode 114二叉树展开为链表
leetcode·链表·深度优先
cz07101 天前
hot100_搜索二维矩阵 II
算法·leetcode
圣保罗的大教堂1 天前
leetcode 3718. 缺失的最小倍数 简单
leetcode
青 春 记 忆1 天前
LeetCode 234. 回文链表|Python 解法详解
python·leetcode·链表
啊嘞嘞?1 天前
力扣(LRU缓存)
算法·leetcode
啊嘞嘞?1 天前
力扣(下一个排列)
算法·leetcode
Forever Nore1 天前
LeetCode 16 最接近的三数之和 - 双指针逼近
算法·leetcode·职场和发展
smj2302_796826521 天前
解决leetcode第4033题有效K个不同元素子数组I
数据结构·python·算法·leetcode