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;
    }
};
相关推荐
文丑颜不良啊9 小时前
LeetCode3281~3300题目解析
java·leetcode
文丑颜不良啊12 小时前
LeetCode3261~3280题目解析
java·leetcode
Rain的Java大神之路14 小时前
LeetCode 40 组合总和 II:面试官想听的不是回溯,而是那行去重代码
java·leetcode·面试
ashiho1 天前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
Pniubi2 天前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
文丑颜不良啊2 天前
LeetCode3041~3060题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode3101~3120题目解析
java·leetcode
玄昌盛不会编程2 天前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
文丑颜不良啊2 天前
LeetCode3061~3080题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode3081~3100题目解析
java·leetcode