• 力扣labuladong——一刷day01


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


    前言


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

    /**
     * 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    二、力扣86. 分隔链表

    /**
     * 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

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

    /**
     * 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

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

    /**
     * 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    web前端设计与开发期末作品_期末大作业【使用HTML制作汽车首页】
    数据结构<4>栈和队列——原理+实现
    Nginx安装
    E. Prefix Function Queries(KMP)
    CSS 盒子模型
    【分享】Excel“只读方式”的两种模式
    mysql 普通索引 limit 慢的问题
    R语言 线性回归的有关方法
    SJF抢占式C语言代码
    MySQL Insert 后獲得主鍵
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/133970529