day38(12.19)——leetcode面试经典150

92. 反转链表 II

92. 反转链表Ⅱ

题目:

题解:

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode cur = head;
        ListNode part = new ListNode();
        int count = 1;
        Map<Integer, ListNode> map = new HashMap<>();
        while(cur != null) {
            map.put(count++, new ListNode(cur.val));
            cur = cur.next;
        } 
        cur = new ListNode();
        ListNode h = cur;
        for(int i=1;i<left;i++) {
            cur.next = map.get(i);
            cur = cur.next;
        }
        for(int i=right;i>=left;i--) {
            cur.next = map.get(i);
            cur = cur.next;
        }
        for(int i=right+1;i<count;i++) {
            cur.next = map.get(i);
            cur = cur.next;
        }
        return h.next;
    }
}
相关推荐
AIpanda8884 小时前
如何借助AI销冠系统提升数字员工在销售中的成效?
算法
啊阿狸不会拉杆4 小时前
《机器学习导论》第 7 章-聚类
数据结构·人工智能·python·算法·机器学习·数据挖掘·聚类
木非哲4 小时前
机器学习--从“三个臭皮匠”到 XGBoost:揭秘 Boosting 算法的“填坑”艺术
算法·机器学习·boosting
小辉同志5 小时前
437. 路径总和 III
算法·深度优先·广度优先
笨笨阿库娅5 小时前
从零开始的算法基础学习
学习·算法
不想睡觉_5 小时前
优先队列priority_queue
c++·算法
那个村的李富贵13 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
power 雀儿13 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
琹箐14 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia114 小时前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱