链表面试编程题

news/2024/10/4 2:35:34 标签: 链表, 面试, 数据结构

1. 删除链表中等于给定值 val 的所有节点。 203. 移除链表元素 - 力扣(LeetCode)

2. 反转一个单链表206. 反转链表 - 力扣(LeetCode)

3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回

第二个中间结 点。876. 链表的中间结点 - 力扣(LeetCode)

4. 输入一个链表,输出该链表中倒数第k个结点。面试题 02.02. 返回倒数第 k 个节点 - 力扣(LeetCode)

5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。21. 合并两个有序链表 - 力扣(LeetCode)

6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。链表分割_牛客题霸_牛客网 (nowcoder.com)

7. 链表的回文结构。链表的回文结构_牛客题霸_牛客网 (nowcoder.com)

 8. 输入两个链表,找出它们的第一个公共结点160. 相交链表 - 力扣(LeetCode)

9. 给定一个链表,判断链表中是否有环。 141. 环形链表 - 力扣(LeetCode)

10.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL

142. 环形链表 II - 力扣(LeetCode)

 1. 删除链表中等于给定值 val 的所有节点。 203. 移除链表元素 - 力扣(LeetCode)​​​​​

class Solution {
    public ListNode removeElements(ListNode head, int val) {
      if(head==null){
            return head;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==val){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==val){
            head=head.next;
        }
        return head;
}
}

 

 2. 反转一个单链表206. 反转链表 - 力扣(LeetCode)

 方法一:头插法

class Solution {//方法一:头插法
    public ListNode reverseList(ListNode head) {
        if(head==null){
            return head;
        }
        ListNode   cur=head.next;
        head.next=null; 
        while(cur!=null){
                ListNode curN=cur.next;
                cur.next=head;
                head=cur;
                cur=curN;

        }
        return head;
    }
}

方法二:改变指向法

/**
 * 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) {
        if(head==null){
            return head;
        }
        ListNode save;
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            save=cur.next;
            cur.next=head;
        head=cur;
        cur=save;
        }
        return head;

    }
}

 3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结 点。876. 链表的中间结点 - 力扣(LeetCode)

方法一:快慢指针法

class Solution {
    public ListNode middleNode(ListNode head) {

        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;

    }
}

 4. 输入一个链表,输出该链表中倒数第k个结点。面试题 02.02. 返回倒数第 k 个节点 - 力扣(LeetCode)

方法:依旧是快慢指针法,快指针先走k-1步,紧接着一起走,快指针走到null时,slow指针的值就是倒数第k个节点,需要注意的是,确保k符合条件

class Solution {
    public int kthToLast(ListNode head, int k) {
        ListNode slow=head;
        ListNode fast=head;
        while(k-1>0){
            fast=fast.next;
            k--;
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;

    }
}

 5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。21. 合并两个有序链表 - 力扣(LeetCode)

方法:创建傀儡节点/虚拟节点,小的进行尾插

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode head=new ListNode(0);
        ListNode cur=head;
        while(list1!=null&&list2!=null){
            if(list1.val>list2.val){
                 cur.next=list2;
                list2=list2.next;
                cur=cur.next;
             
         
            }else{
                  cur.next=list1;
                list1=list1.next;
                cur=cur.next;
            }
        }

        if(list1!=null){
        cur.next=list1;
       
          
        }
        if(list2!=null){
        cur.next=list2;
    
        }
        return head.next;

    }
}

 6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。链表分割_牛客题霸_牛客网 (nowcoder.com)

方法:依旧使用两个虚拟节点/傀儡节点,小于x的放在一个虚拟节点内,大于等于x的放在另一个虚拟节点内,最后进行拼接

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        ListNode nx=new ListNode(0);
        ListNode xn=new ListNode(0);
        ListNode cur=pHead;
        ListNode as=nx;
        ListNode bs=xn;
        while(cur!=null){
            if(cur.val<x){
                nx.next=cur;
                cur=cur.next;
                nx=nx.next;
            }else{
                xn.next=cur;
                cur=cur.next;
                xn=xn.next;
            }
        }
        nx.next=bs.next;
        xn.next=null;
        return as.next;
    }
}

 7. 链表的回文结构。链表的回文结构_牛客题霸_牛客网 (nowcoder.com)

先寻找中间节点

再从中间节点往后翻转

从后往前循环判断,判断过程中分奇偶判断,内补实现偶数判断返回true,循环结束为奇数判断结束,返回true

    public boolean chkPalindrome(ListNode A) {
        if(A==null){
            return true;
        }
        // write code here
        ListNode slow=A;
        ListNode fast=A;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
    ListNode cur=slow.next;
    while(cur!=null){
        ListNode curN=cur.next;
        cur.next=slow;
        slow=cur;
        cur=curN;
    }//从中间节点开始,翻转另一边链表
    while(A!=slow){
        if(A.val!=slow.val){
            return false;
        }
        if(A.next==slow){//偶数特例
            return true;
        }
        A=A.next;
        slow=slow.next;
    }
    return true;//奇数例子
    }

也可以不用判断奇数偶数,及时给找到中间节点后的slow的next置空就好啦

8. 输入两个链表,找出它们的第一个公共结点160. 相交链表 - 力扣(LeetCode)

ublic ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl=headA;
        ListNode ps=headB;
        int countA=0;
        int countB=0;
        while(pl!=null){
            countA++;
            pl=pl.next;
        }
        while(ps!=null){
            countB++;
            ps=ps.next;
        }
        int len=countA-countB;
        pl=headA;
        ps=headB;
        if(len<0){
            len=countB-countA;
            pl=headB;
            ps=headA;
        }
        while(len>0){
            pl=pl.next;
            len--;
        }
        while(pl!=ps){
            pl=pl.next;
            ps=ps.next;
        }
        if(pl==null){
            return null;
        }
        return pl;
    }

 9. 给定一个链表,判断链表中是否有环。141. 环形链表 - 力扣(LeetCode)

  public boolean hasCycle(ListNode head) {
        if(head==null){
            return false;
        }
            ListNode fast=head;
            ListNode slow=head;
            while(fast!=null&&fast.next!=null){
                slow=slow.next;
                fast=fast.next.next;
                if(fast==slow){
                        return true;
                }
            }
            return false;
    }

 10.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL

142. 环形链表 II - 力扣(LeetCode)

结论
让一个指针从链表起始位置开始遍历链表,同时让一个指针从判环时相遇点的位置开始绕环运行,两个指针 都是每次均走一步,最终肯定会在入口点的位置相遇
证明:
  public ListNode detectCycle(ListNode head) {
         if(head==null){
            return null;
        }
            ListNode Head=head;
            ListNode fast=head;
            ListNode slow=head;
            while(fast!=null&&fast.next!=null){
                slow=slow.next;
                fast=fast.next.next;
                if(fast==slow){
                        while(Head!=slow){
                            Head=Head.next;
                            slow=slow.next;
                            
                        }
                        if(slow==Head){
                                return Head;
                            }
                }
            }
            return null;
    }


http://www.niftyadmin.cn/n/5689315.html

相关文章

Spring 的 IOC 和 AOP 是什么,有哪些优点?解密 Spring两大核心概念:IOC与AOP的魅力所在

在现代Java开发中&#xff0c;Spring框架几乎是不可或缺的存在。它不仅简化了开发过程&#xff0c;还提高了软件的灵活性和可维护性。今天&#xff0c;我们要深入探讨Spring中的两个核心概念&#xff1a;IOC&#xff08;控制反转&#xff09;和AOP&#xff08;面向切面编程&…

C#的面向对象

1&#xff09;对象 算法数据结构 2&#xff09;对象的行为已方法的形式定义的&#xff0c;属性以成员变量的形式定义的 面向对象程序设计的特点 1&#xff09;封装性 2&#xff09;继承性 3&#xff09;多态性 知识点&#xff1a; 封装性面向对象的核心思想&#xff0c;将…

k8s 部署 grafana

创建namespace grafana-namespace.yaml apiVersion: v1 kind: Namespace metadata:name: ns-grafana拉取镜像 swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/rancher/mirrored-grafana-grafana:10.3.3grafana的Deployment grafana-deployment.yaml apiVersion: app…

命令按钮QCommandLinkButton

主要作用&#xff1a;用来点击后可以自动打开系统的网页浏览器&#xff0c;跳转到指定的网页 常用方法 文本 //获取和设置文本 QString text() const void setText(const QString &text)描述信息 //获取和设置描述文本 QString description() const void setDescripti…

CSS多列

CSS多列 前言 有的时候希望文本能按照多列效果显示&#xff0c;如&#xff1a; 这时候就要把文本显示效果改成多列显示&#xff0c;标题独占一行 CSS文本多列使用 ① column-count 指定文本分为几列&#xff0c;如&#xff1a; column-count: 3;② column-gap 指定列之…

Python | Leetcode Python题解之第450题删除二叉搜索树中的节点

题目&#xff1a; 题解&#xff1a; class Solution:def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:cur, curParent root, Nonewhile cur and cur.val ! key:curParent curcur cur.left if cur.val > key else cur.rightif cur i…

负载均衡--相关面试题(六)

在负载均衡的面试中&#xff0c;可能会遇到一系列涉及概念、原理、实践应用以及技术细节的问题。以下是一些常见的负载均衡面试题及其详细解答&#xff1a; 一、什么是负载均衡&#xff1f; 回答&#xff1a;负载均衡是一种将网络请求或数据传输工作分配给多个服务器或网络资源…

mysql学习教程,从入门到精通,SQL 表的创建(33)

1、SQL 表的创建 在SQL中&#xff0c;创建表的基本语法是使用CREATE TABLE语句。以下是一个基本的CREATE TABLE语法模板&#xff0c;以及对其各个部分的解释&#xff1a; CREATE TABLE 表名 (列名1 数据类型 [约束条件] [默认值],列名2 数据类型 [约束条件] [默认值],...[表级…