知识改变命运 数据结构【链表面试题】

server/2024/9/24 10:38:57/

1. 删除链表中等于给定值 val 的所有节点。 OJ链接

在这里插入图片描述

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

在这里插入图片描述

2. 反转一个单链表。 OJ链接

在这里插入图片描述

class Solution {public ListNode reverseList(ListNode head) {if (head == null) {return null;}ListNode cur = head.next;head.next = null;while (cur != null) {ListNode C = cur.next;cur.next = head;head = cur;cur = pre;}return head;}}

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

点。OJ链接
在这里插入图片描述

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

4. 输入一个链表,输出该链表中倒数第k个结点。 OJ链接

在这里插入图片描述

方法1

 public int kthToLast2( int k) {if(k <= 0 ) {return -1;}ListNode fast = head;ListNode slow = head;int count = 0;while (count != k-1) {fast = fast.next;if(fast == null) {return -1;}count++;}while (fast.next != null) {fast = fast.next;slow = slow.next;}return slow.val;}

方法2

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

5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。OJ

在这里插入图片描述

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

6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。OJ链接

在这里插入图片描述

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

7. 链表的回文结构。OJ链接

在这里插入图片描述

public class PalindromeList {public boolean chkPalindrome(ListNode head) {ListNode fast=head;ListNode slow=head;while(fast!=null&&fast.next!=null) {slow=slow.next;fast=fast.next.next;}ListNode cur=slow.next;ListNode curN=cur;while(curN!=null) {curN=curN.next;cur.next=slow;slow=cur;cur=curN;}while(slow!=head) {if(head.val!=slow.val) {return false;}if (head.next==slow) {return true;}head=head.next;slow=slow.next;}return true;}
}

8. 输入两个链表,找出它们的第一个公共结点。OJ链接

在这里插入图片描述

public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode pl=headA;ListNode ps=headB;int len1=size(pl);int len2=size(ps);int leng=Math.abs(len1-len2);if(len1>len2) {while(leng!=0) {pl=pl.next;leng--;}}else {while (leng !=0) {ps=ps.next;leng--;}}while(ps!=null&&pl!=null) {if(ps==pl) {return ps;}ps=ps.next;pl=pl.next;}return null;}public int size(ListNode head) {int count=0;while (head!=null) {head=head.next;count++;}return count;}
}

9. 给定一个链表,判断链表中是否有环。 OJ链接

在这里插入图片描述

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

【思路】
快慢指针,即慢指针一次走一步,快指针一次走两步,两个指针从链表起始位置开始运行,如果链表
带环则一定会在环中相遇,否则快指针率先走到链表的末尾。比如:陪女朋友到操作跑步减肥。
【扩展问题】
为什么快指针每次走两步,慢指针走一步可以?
假设链表带环,两个指针最后都会进入环,快指针先进环,慢指针后进环。当慢指针刚进环时,可能就和快
指针相遇了,最差情况下两个指针之间的距离刚好就是环的长度。此时,两个指针每移动一次,之间的距离
就缩小一步,不会出现每次刚好是套圈的情况,因此:在慢指针走到一圈之前,快指针肯定是可以追上慢指
针的,即相遇。
快指针一次走3步,走4步,…n步行吗?
在这里插入图片描述
按上面的方法,每次快指针走三步,慢指针走一步,永远不会相遇,因为快指针把慢指针套圈了。
因此只有快指针走2步,慢指针走一步,即使慢指针被套圈,slow和fast也是同一位置。

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

在这里插入图片描述

public class Solution {public ListNode detectCycle(ListNode head) {if(head==null) {return null;}ListNode fast=head;ListNode slow=head;while (fast!=null&&fast.next!=null) {fast=fast.next.next;slow=slow.next;if(slow==fast) {break;}}if(fast==null||fast.next==null) {return null;}fast=head;while (fast!=slow) {fast=fast.next;slow=slow.next;}return fast;}
}

在这里插入图片描述


http://www.ppmy.cn/server/103838.html

相关文章

学习之appium的简单使用

使用之前需要先安装一下依赖 1、安装jdk&#xff1a;暂时为整理笔记以后补充 2、安装nodejs:https://blog.csdn.net/qq_42792477/article/details/141363957?spm1001.2014.3001.5501 3、安装SDk&#xff08;安卓篇&#xff09;&#xff1a;https://blog.csdn.net/qq_42792477…

esbuild中的Data URL Loader:轻松将文件嵌入为Base64编码的数据URL

在前端项目中&#xff0c;经常需要将图片、字体、音频或其他文件直接嵌入到JavaScript代码中&#xff0c;以便在运行时能够立即使用&#xff0c;而无需额外的HTTP请求。为了实现这一需求&#xff0c;esbuild提供了Data URL Loader这一功能强大的工具。它能够在构建时将文件加载…

Prism-学习笔记1-安装Prism

安装Prism 在VS2022中安装如下图&#xff1a; 2. 搜索Prism&#xff0c;安装Prism&#xff1a;&#xff08;ps&#xff1a;如果安装很慢&#xff0c;直接往上搜关键字 Prism template Pack 下载&#xff0c;或者这里我下载好的Prism包&#xff0c;提取码&#xff1a;bi7c&…

Qt第十七章 多线程

文章目录 多线程1. 线程概念的起源2. 三种方式创建线程3. 启动线程前的准备工作4. 启动线程/退出线程5. 操作运行中的线程6. 为每个线程提供独立数据7.子线程不能操作ui解决方案 多线程 1. 线程概念的起源 单核CPU 早期还没有线程的概念&#xff0c;如何保证2个进程同时进行呢…

android13顶部状态栏里面调节背光 背景闪烁问题

总纲 android13 rom 开发总纲说明 目录 1.前言 2.问题分析 3.代码分析 4.代码修改 5.彩蛋 1.前言 android13顶部状态栏里面调节背光, 背景闪烁问题,会出现画面不全问题,如下图 2.问题分析 这里看起来是由于隐藏的时候,界面显示是一个渐变的隐藏,但是后面的背景又是…

QT基础知识5

思维导图 client.cpp #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget), socket(new QTcpSocket(this))//给客户端实例化分配空间 {ui->setupUi(this);//初始化界面ui->msgEdit-&…

pair 与 tie功能与用法-清晰教程

目录 一、什么是 pair 1.1 定义 1.2 功能 1.3 使用方法 1.3.1 创建 pair 1.3.2 用法场景 1.3.3 其他操作 二、什么是 tie 2.1 定义 2.2 功能 2.3 使用方法 2.3.1 基本用法 2.3.2 忽略部分值 2.3.3 使用场景 三、pair 与 tie 的比较 3.1 功能比较 3.2 用途比较…

PCIe学习笔记(26)

Error Forwarding&#xff08;错误转发&#xff09; 错误转发(也称为数据中毒)&#xff0c;通过设置EP位表示。下面是一些使用错误转发的例子: •例#1:从主存读取遇到不可纠正的错误 •例#2:PCI写到主存的奇偶校验错误 •例#3:内部数据缓冲区或缓存上的数据完整性错误 错误…