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;
    }
};
相关推荐
Mr_Xuhhh4 小时前
项目需求分析(2)
c++·算法·leetcode·log4j
Morri35 小时前
[Java恶补day53] 45. 跳跃游戏Ⅱ
java·算法·leetcode
林木辛5 小时前
LeetCode热题 15.三数之和(双指针)
算法·leetcode·双指针
siy23337 小时前
[c语言日记] 数组的一种死法和两种用法
c语言·开发语言·笔记·学习·链表
和光同尘@8 小时前
66. 加一 (编程基础0到1)(Leetcode)
数据结构·人工智能·算法·leetcode·职场和发展
CHEN5_028 小时前
leetcode-hot100 11.盛水最多容器
java·算法·leetcode
songx_998 小时前
leetcode18(无重复字符的最长子串)
java·算法·leetcode
我爱996!9 小时前
LinkedList与链表
数据结构·链表
Lris-KK10 小时前
【Leetcode】高频SQL基础题--1341.电影评分
sql·leetcode
B612 little star king11 小时前
力扣29. 两数相除题解
java·算法·leetcode