• 【力扣/牛客刷题】203. 移除链表元素 || 206. 反转链表


    作者:✿✿ xxxflower. ✿✿
    博客主页:xxxflower的博客
    专栏:【力扣/牛客刷题】篇
    语录:⭐每一个不曾起舞的日子,都是对生命的辜负。⭐

    203.移除链表元素

    题目OJ链接:203. 移除链表元素
    在这里插入图片描述
    【题目分析】我们先来分析一下我们需要注意什么

    1. 链表为空
    2. 链表中只有一个数字
    3. 所要删除的数字位于链表中间(即最普通的情况)
    4. 链表中要寻找的数字从头开始出现多次(例如:23 23 23 45 23)(删除23)

    我们先设置两个值:

    1. cur:代表当前需要删除的节点
    2. prev:代表当前需要删除的节点的前驱
      在这里插入图片描述
      我们要怎么删除34呢?步骤如下:
      请添加图片描述
      在这里插入图片描述
      如果不是所要求的值,则执行else往后走。直到cur == null
      综上我们可以得到代码:
    /**
     * 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 removeElements(ListNode head, int val) {
            //1. 链表为空
            if(head == null){
                return null;
            }
            //2. 所要删除的数字位于链表中间(即最普通的情况)
            ListNode cur = head.next;
            ListNode prev = head;
            while(cur != null){
                if(cur.val == val){
                    prev.next = cur.next;
                    cur = cur.next;
                }else{
                    prev = cur;
                    cur = cur.next;
                }
            }
            //3. 链表中要寻找的数字从头开始出现多次(例如:23 23 23 45  23)(删除23)此时以上代码执行完时,还有第一个没有删除,因此需要在判断一下
            if(head.val == val){
                head = head.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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述

    206.反转链表

    题目OJ链接:206. 反转链表
    在这里插入图片描述
    【题目分析】
    在这里插入图片描述

    1. 先把0x89存起来,方便之后与后续链表产生关系
      ListNode cur = head.next;
    2. 然后将0x89置为空
      head.next = null;
    3. 由以下代码得:

    请添加图片描述

    此处的代码为本题的关键,如果还不理解可以调试一下,一步步走。
    在这里插入图片描述

    /**
     * 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 reverseList(ListNode head) {
            //1.没有节点
            if(head == null){
                return null;
            }
            //2.只有一个节点
            if(head.next == null){
                return head;
            }
            ListNode cur = head.next;
            head.next = null;
            while(cur != null){
                ListNode curNext = cur.next;
                cur.next = head;
                head = cur;
                cur = curNext;
            }
            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
    • 29
    • 30
    • 31

    在这里插入图片描述

    876. 链表的中间结点

    题目OJ链接:876.链表的中间结点
    在这里插入图片描述
    【题目分析】本题可以利用快慢指针来完成。
    在这里插入图片描述

    /**
     * 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 middleNode(ListNode head) {
            ListNode fast = head;
            ListNode slow = head;
            while((fast != null)&&(fast.next != null)){
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    链表中倒数第k个结点

    链表中倒数第k个结点
    在这里插入图片描述
    在这里插入图片描述

    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    public class Solution {
        public ListNode FindKthToTail(ListNode head,int k) {
            if(k < 0 ||head == null){
                 return null;
            }
            ListNode fast = head;
            ListNode slow = head;
            while(k-1 != 0){
                 fast = fast.next;
                if(fast == null){
                    return null;
                }
                k--;
            }
            while(fast.next != null){
                fast = fast.next; 
                slow = slow.next;
            }
            return slow;
    
        }
    }
    
    
    • 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

    21. 合并两个有序链表

    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 head1,
         ListNode head2) {
            ListNode newHead = new ListNode(-1);
            ListNode tmp = newHead;
            while(head1 != null && head2 != null){
                if(head1.val < head2.val){
                    tmp.next = head1;
                    head1 = head1.next;
                }else{
                    tmp.next = head2;
                    head2 = head2.next;
                }
                tmp = tmp.next;
            }
            if(head1 != null){
                tmp.next = head1;
            }
            if(head2 != null){
                tmp.next = head2;
            }
            return newHead.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

    CM11 链表分割

    CM11 链表分割
    在这里插入图片描述
    在这里插入图片描述

    import java.util.*;
    
    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    public class Partition {
        public ListNode partition(ListNode pHead, int x) {
            // write code here
            ListNode as=null;
            ListNode ae=null;
            ListNode bs=null;
            ListNode be=null;
            ListNode cur=pHead;
            while(cur!=null){
                if(cur.val>=x){
                    if(as==null){
                        as=cur;
                        ae=cur;
                    }else{
                        ae.next=cur;
                        ae=ae.next;
                    }
                }else{
                    if(bs==null){
                        bs=cur;
                        be=cur;
                    }else{
                        be.next=cur;
                        be=be.next;
                    }
                }
                cur=cur.next;
            }
            if(bs==null){
                return as;
            }
            be.next=as;
            if(as!=null){
                ae.next=null;
            }
            return bs;
        }
    }
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    OR36 链表的回文结构

    题目Oj:OR36 链表的回文结构
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    import java.util.*;
    
    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    public class PalindromeList {
        public boolean chkPalindrome(ListNode head) {
            // write code here
            if(head == null){
                return false;
            }
            if(head.next == null){
                return true;
            }
            ListNode fast = head;
            ListNode slow = head;
            while(fast != null && fast.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }
            ListNode cur = slow.next;;
            while(cur != null){
                ListNode curNext = cur.next;
                cur.next = slow;
                slow = cur;
                cur = curNext;
            }
            while(head != slow){
                if(head.val != slow.val){
                    return false;
                }
                if(head.next == slow){
                    return true;
                }
                head = head.next;
                slow = slow.next;
            }
            return true;
        }
    }
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    160. 相交链表

    160. 相交链表
    在这里插入图片描述

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            // 1.计算A和B链表的长度
            int headAlen=headLen(headA);
            int headBlen=headLen(headB);
            int n=0;
            // 2.求其差值并让长的链表走差值步
            if(headAlen>headBlen){
                n=headAlen-headBlen;
            }else{
                n=headBlen-headAlen;
            }
            if(headAlen>headBlen){
                while(n!=0){
                    headA=headA.next;
                    n--;
                }
            }else{
                while(n!=0){
                    headB=headB.next;
                    n--;
                }
            }
            // 3.两个链表同时走,如果相等则返回即为相遇值
            while(headA!=null && headB!=null){
                if(headA==headB){
                    return headA;
                }
                headA=headA.next;
                headB=headB.next;
            }
            return null;
        }
        // 计算链表长度的函数
        public int headLen(ListNode head){
            int count=0;
            if(head==null){
                return 0;
            }
            while(head!=null){
                count++;
                head=head.next;
            }
            return count;
        }
    }
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    141. 环形链表

    141. 环形链表
    在这里插入图片描述

    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if(head==null){
                return false;
            }
            ListNode fast=head.next;
            ListNode slow=head;
            // 注意此处的条件是快指针不为空
            while(fast!=null &&fast.next!=null){
                fast=fast.next.next;
                slow=slow.next;
                if(fast==slow){
                    return true;
                } 
    
            }
            return false;
        }
    }
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    Java虚拟机运算指令学习
    手机也可以搭建个人博客?安卓Termux+Hexo搭建属于你自己的博客网站【cpolar实现公网访问】
    边缘检测--学习笔记
    IP子网的划分
    [附源码]Python计算机毕业设计Django健身生活系统论文
    项目7-音乐播放器5+注册账号
    【计算机网络】湖科大学习笔记---计算机网络体系结构
    机器学习中常用的评价指标
    关于本地项目上传到gitee的详细流程
    多个输入框实现串联筛选
  • 原文地址:https://blog.csdn.net/qq_61138087/article/details/127650687