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;
    }
};
相关推荐
All for pursuit.1 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
CoderYanger5 小时前
A.每日一题:1614. 括号的最大嵌套深度
java·程序人生·算法·leetcode·面试·学习方法
Xin7706 小时前
LeetCode 230. 二叉搜索树中第 K 小的元素(迭代中序遍历 + 栈)
leetcode
Navigator_Z7 小时前
LeetCode //C - 1283. Find the Smallest Divisor Given a Threshold
c语言·算法·leetcode
我不会起名字3227 小时前
二叉树的遍历与二叉搜索树:把递归、迭代、还原树一次讲透
数据结构·leetcode·二叉树·二叉搜索树
yi0118 小时前
Leetcode 49 用 “身份证‘‘巧解
人工智能·笔记·python·算法·leetcode·哈希表
yi0118 小时前
DAY22: LeetCode 733:图像渲染——从二维网格开始理解 DFS 和 BFS
数据结构·笔记·python·算法·leetcode·深度优先·宽度优先
Pniubi8 小时前
力扣41缺失的第一个正数(哈希表法)
算法·leetcode·职场和发展
Nil20810 小时前
leetcode 70爬楼梯
算法·leetcode·动态规划
longlongzihan11 小时前
LeetCode 189. 轮转数组:从直观模拟到最优原地算法
c++·算法·leetcode