力扣labuladong——一刷day01

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、21. 合并两个有序链表](#一、21. 合并两个有序链表)
  • [二、力扣86. 分隔链表](#二、力扣86. 分隔链表)
  • [三、力扣23. 合并 K 个升序链表](#三、力扣23. 合并 K 个升序链表)
  • [四、力扣删除链表的倒数第 N 个结点](#四、力扣删除链表的倒数第 N 个结点)

前言


一、21. 合并两个有序链表

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 mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode res = new ListNode(), pre = res;
        ListNode p1 = list1, p2 = list2;
        while(p1 != null && p2 != null){
            if(p1.val <= p2.val){
                pre.next = p1;
                p1 = p1.next;
                pre = pre.next; 
            }else{
                pre.next = p2;
                p2 = p2.next;
                pre = pre.next;
            }
        }
        while(p1 != null){
            pre.next = p1;
            p1 = p1.next;
            pre = pre.next;
        }
        while(p2 != null){
            pre.next = p2;
            p2 = p2.next;
            pre = pre.next;
        }
        return res.next;
    }
}

二、力扣86. 分隔链表

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 partition(ListNode head, int x) {
        ListNode dump1 = new ListNode(-1,null);
        ListNode dump2 = new ListNode(-1,null);
        ListNode p = head, p1 = dump1, p2 = dump2;
        while(p != null){
            if(p.val < x){
                p1.next = p;
                p1 = p1.next;
            }else{
                p2.next = p;
                p2 = p2.next;
            }
            ListNode temp = p.next;
            p.next = null;
            p = temp;
        }
        p1.next = dump2.next;
        return dump1.next;
    }
}

三、力扣23. 合并 K 个升序链表

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) {
        if(lists.length == 0){
            return null;
        }
        ListNode res = new ListNode(-1,null);
        ListNode r = res;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(
            lists.length, (a,b)->(a.val - b.val)
        );
        for(ListNode node : lists){
            if(node != null){
                pq.add(node);
            }
        }
        while(!pq.isEmpty()){
            ListNode p = pq.poll();
            r.next = p;
            if(p.next != null){
                pq.add(p.next);
            }
            r = r.next;
        }
        return res.next;
    }
}

四、力扣删除链表的倒数第 N 个结点

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 removeNthFromEnd(ListNode head, int n) {
        ListNode p1 = head;
        for(int i = 0; i < n; i ++){
            p1 = p1.next;
        }
        if(p1 == null){
            return head.next;
        }
        ListNode p2 = head;
        while(p1.next != null){
            p1 = p1.next;
            p2 = p2.next;
        }
        p2.next = p2.next.next;
        return head;
    }
}
相关推荐
测试19983 小时前
软件测试 - 单元测试总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
Mahir085 小时前
Spring 循环依赖深度解密:从问题本质到三级缓存源码级解析
java·后端·spring·缓存·面试·循环依赖·三级缓存
RyFit6 小时前
SpringAI 常见问题及解决方案大全
java·ai
石山代码7 小时前
C++ 内存分区 堆区
java·开发语言·c++
心中有国也有家7 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
绝知此事7 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
无风听海7 小时前
C# 隐式转换深度解析
java·开发语言·c#
碧海银沙音频科技研究院7 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
一只大袋鼠8 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
csdn_aspnet8 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展