力扣-Hot100-链表其三【算法学习day.36】

前言

###我做这类文档一个重要的目的还是给正在学习的大家提供方向(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.随机链表的复制

题目链接: 138. 随机链表的复制 - 力扣(LeetCode)

题面:

**基本分析:**主要难在random的处理上,我看题解的

代码:

java 复制代码
/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/

class Solution {
    public Node copyRandomList(Node head) {
       for(Node i = head;i!=null;i=i.next){
          Node flag = new Node(i.val);
          flag.next = i.next;
          i.next = flag;
          i=i.next;
       }
       for(Node i = head;i!=null;i=i.next){
         if(i.random!=null){
            i.next.random = i.random.next;
         }
         i = i.next;
       }
       Node root = new Node(-1);
       Node node = new Node(-1);
       root.next = node;
       for(Node i = head;i!=null;i=i.next){
        node.next = i.next;
        i.next = node.next.next;
        node = node.next;
       }
       return root.next.next;
    }
}

2.排序链表

题目链接: 148. 排序链表 - 力扣(LeetCode)

题面:

**基本分析:**我是先把所有值存起来然后构建链表的暴力写法

代码:

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 sortList(ListNode head) {
        int[] arr = new int[50005];
        int count =  0;
        for(ListNode i = head;i!=null;i=i.next){
            arr[count++] = i.val;
        }
        Arrays.sort(arr,0,count);
        ListNode p = head;
        for(int i = 0;i<count;i++){
            p.val = arr[i];
            p = p.next;
        }
        return head;
    }
}

3.LRU缓存

题目链接: 146. LRU 缓存 - 力扣(LeetCode)

题面:

**基本分析:**把整个过程想象成一叠书,可以看看灵神的题解

代码:

java 复制代码
class LRUCache {
    private static class Node {
        int key, value;
        Node prev, next;

        Node(int k, int v) {
            key = k;
            value = v;
        }
    }

    private final int capacity;
    private final Node dummy = new Node(0, 0); // 哨兵节点
    private final Map<Integer, Node> keyToNode = new HashMap<>();

    public LRUCache(int capacity) {
        this.capacity = capacity;
        dummy.prev = dummy;
        dummy.next = dummy;
    }

    public int get(int key) {
        Node node = getNode(key);
        return node != null ? node.value : -1;
    }

    public void put(int key, int value) {
        Node node = getNode(key);
        if (node != null) { // 有这本书
            node.value = value; // 更新 value
            return;
        }
        node = new Node(key, value); // 新书
        keyToNode.put(key, node);
        pushFront(node); // 放在最上面
        if (keyToNode.size() > capacity) { // 书太多了
            Node backNode = dummy.prev;
            keyToNode.remove(backNode.key);
            remove(backNode); // 去掉最后一本书
        }
    }

    // 获取 key 对应的节点,同时把该节点移到链表头部
    private Node getNode(int key) {
        if (!keyToNode.containsKey(key)) { // 没有这本书
            return null;
        }
        Node node = keyToNode.get(key); // 有这本书
        remove(node); // 把这本书抽出来
        pushFront(node); // 放在最上面
        return node;
    }

    // 删除一个节点(抽出一本书)
    private void remove(Node x) {
        x.prev.next = x.next;
        x.next.prev = x.prev;
    }

    // 在链表头添加一个节点(把一本书放在最上面)
    private void pushFront(Node x) {
        x.prev = dummy;
        x.next = dummy.next;
        x.prev.next = x;
        x.next.prev = x;
    }
}

4.合并k个升序链表

题目链接: 23. 合并 K 个升序链表 - 力扣(LeetCode)

题面:

**基本分析:**暴力做法还是很好做的,把所有值存数组,排序后构造一个数组并返回

代码:

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 mergeKLists(ListNode[] lists) {
        int[] arr = new int[10005];
        int count = 0;
        for(ListNode node : lists){
            for(ListNode i = node;i!=null;i=i.next){
                arr[count++] = i.val;
            }
        }
        Arrays.sort(arr,0,count);
        ListNode root = new ListNode(0);
        ListNode node = new ListNode(0);
        root.next = node;
        for(int i = 0;i<count;i++){
            ListNode flag = new ListNode(arr[i]);
            node.next = flag;
            node = node.next;
        }
        return root.next.next;
    }
}

后言

上面是力扣Hot100的链表专题,下一篇是其他专题的习题,希望有所帮助,一同进步,共勉!

相关推荐
高晶1 分钟前
无乘法器实现LC二阶滤波器:基于移位运算的快速算法与四种精度推导
算法·设计模式·程序员
ysbfsy4 分钟前
大数计算思路
算法
高晶15 分钟前
主从自动分配算法:平均值乘以设备数一半的选举基准值解析
算法·设计模式·程序员
学电子她就能回来吗40 分钟前
LeetCode Hot 100|普通数组专题学习笔记(零基础版)
笔记·算法·leetcode
用户老李42 分钟前
反向传播计算梯度举例
算法
niyongsheng43 分钟前
多智能体连续时间内路径规划:AA-SIPP(m) 的 Rust 实现与算法
算法·rust·机器人
卿卿的产品经理日记2 小时前
【AI产品经理实战】Day 38|try-except 踩坑实录:报错是数据
人工智能·笔记·python·学习·产品经理
keyipatience2 小时前
TypeScript学习(1)
前端·javascript·学习·typescript·html·css3
302wanger2 小时前
DeepSeek-V4.1-Flash-1017 免费用到1024,速来
算法
lisw052 小时前
微分与积分的本质区别解析
算法·数学建模·极限编程