• 力扣labuladong——一刷day03


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


    前言


    一、力扣LCR 140. 训练计划 II

    /**
     * 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 trainingPlan(ListNode head, int cnt) {
            ListNode p1 = head, p2;
            for(int i = 0; i < cnt; i ++){
                p1 = p1.next;
            }
            p2 = head;
            while(p1 != null){
                p1 = p1.next;
                p2 = p2.next;
            }
            return p2;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    二、力扣LCR 142. 训练计划 IV

    /**
     * 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 trainningPlan(ListNode l1, ListNode l2) {
            ListNode dump = new ListNode(-1, null), pre = dump;
            while(l1 != null && l2 != null){
                if(l1.val < l2.val){
                    pre.next = l1;
                    pre = pre.next;
                    l1 = l1.next;
                }else{
                    pre.next = l2;
                    pre = pre.next;
                    l2 = l2.next;
                }
            }
            if(l1 != null){
                pre.next = l1;
            }
            if(l2 != null){
                pre.next = l2;
            }
            return dump.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

    三、力扣LCR 171. 训练计划 V

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    class Solution {
        ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode p1 = headA, p2 = headB;
            while(p1 != p2){
                if(p1 == null) p1 = headB;
                else p1 = p1.next;
    
                if(p2 == null) p2 = headA;
                else p2 = p2.next;
            }
            return p1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    四、力扣LCR 021. 删除链表的倒数第 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 p = new ListNode(-1,head);
            ListNode r = fun(p,n+1);
            r.next = r.next.next;
            return p.next;
        }
        public ListNode fun(ListNode head, int n){
            ListNode p = head;
            int i = 1;
            while(i < n){
                i ++;
                p = p.next;
            }
            ListNode res = head;
            while(p.next != null){
                p = p.next;
                res = res.next;
            }
            return res;
        }
    }
    
    • 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

    五、力扣LCR 022. 环形链表 II

    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode detectCycle(ListNode head) {
            ListNode fast = head, slow = head;
            while(slow != null && fast != null && fast.next != null){
                fast = fast.next.next;
                slow = slow.next;
                if(fast == slow){
                    slow = head;
                    while(slow != fast){
                        slow = slow.next;
                        fast = fast.next;
                    }
                    return fast;
                }
            }
            return null;
        }
    }
    
    • 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
  • 相关阅读:
    数据治理平台项目总结和分析
    计算机存储 和 零拷贝
    Rliger | 完美整合单细胞测序数据(部分交集数据的整合)(三)
    如何隐藏这个 columns: [ { title: ‘操作‘, dataIndex: ‘action‘,
    mysql InnooDb存储引擎的体系结构和逻辑存储结构
    RunApi使用详解
    BI工具:让数据分析井然有序一望而知
    阅读 | 003《知识图谱发展报告2022》(一)知识表示与建模
    C++特殊定制:揭秘cpo与tag_invoke!
    (下)苹果有开源,但又怎样呢?
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/133974937