剑指Offer之Java算法习题精讲链表专项训练
作者:明天一定. 发布时间:2023-11-29 16:31:48
标签:Java,链表,练习
题目一
链表题——链表合并
根据给定的两个升序链表合并为一个新的升序链表
具体题目如下
解法
/**
* 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 a = new ListNode(0),b = a;
while(list1!=null&&list2!=null){
if(list1.val<=list2.val){
a.next = list1;
list1 = list1.next;
}else{
a.next = list2;
list2 = list2.next;
}
a = a.next;
}
if(list1==null){
a.next = list2;
}
if(list2==null){
a.next = list1;
}
return b.next;
}
}
题目二
链表题——查找链表
根据给定的链表头文件判断其中是否有环
具体题目如下
解法一
/**
* 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) {
HashSet<ListNode> set = new HashSet<ListNode>();
while(head!=null){
if(!set.add(head)){
return true;
}
set.add(head);
head = head.next;
}
return false;
}
}
解法二
/**
* 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) {
ListNode fast = head;
ListNode slow = head;
while(fast!=null){
if(fast.next==null) return false;
slow = slow.next;
fast = fast.next.next;
if(fast==slow) return true;
}
return false;
}
}
题目三
链表题——查找数组中元素位置
根据给定的链表头节点查找返回链表入环的第一个节点
具体题目如下
解法一
/**
* 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) {
HashSet<ListNode> set = new HashSet<ListNode>();
while(head!=null){
if(!set.add(head)){
return head;
}
set.add(head);
head = head.next;
}
return null;
}
}
解法二
/**
* 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;
ListNode slow = head;
while(fast!=null){
if(fast.next==null) return null;
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
slow = head;
break;
}
}
while(fast!=null){
if(slow == fast){
return slow;
}
slow = slow.next;
fast = fast.next;
}
return null;
}
}
题目四
链表题——查找链表相交起始节点
根据给定的两个链表头节点按照指定条件查找起始节点
具体题目如下
解法一
/**
* 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) {
HashSet<ListNode> set = new HashSet<ListNode>();
while(headA!=null){
set.add(headA);
headA = headA.next;
}
while(headB!=null){
if(!set.add(headB)){
return headB;
}
set.add(headB);
headB = headB.next;
}
return null;
}
}
解法二
/**
* 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) {
ListNode a = headA, b = headB;
while(a != b){
if(a == null) a = headB;
else a = a.next;
if(b == null) b = headA;
else b = b.next;
}
return a;
}
}
题目五
链表题——链表操作
根据给定的链表删除指定节点并返回头节点
具体题目如下
解法
/**
* 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 node = new ListNode(-1);
node.next = head;
ListNode x = findFromEnd(node,n+1);
x.next = x.next.next;
return node.next;
}
private ListNode findFromEnd(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
for(int i = 0;i<k;i++){
fast = fast.next;
}
while(fast!=null){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
题目六
链表题——查找链表中间节点
根据给定的链表头节点查找其中间节点
具体题目如下
解法
/**
* 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){
if(fast.next == null) return slow;
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
来源:https://blog.csdn.net/wai_58934/article/details/122991115


猜你喜欢
- 对于Android中的手势识别可以从以下三个Listener入手——OnTouchListener、OnGestureListener、On
- 今天本文与大家分享如何得到数组中的最大值和最小值的实例。很适合Java初学者复习数组的基本用法与流程控制语句的使用。具体如下:这个程序主要是
- Java RandomAccessFile 指定位置实现文件读取与写入RandomAccessFile是属于随机读取类,是可以对文件本身的内
- 本文实例讲述了C#实现的字符串转MD5码函数。分享给大家供大家参考,具体如下:/*测试环境:WinXP SP3、Visual Studio
- 前提在Windows下进行数据处理的时候最常见的情况莫过于读取Microsoft的Excel文件了,Excel的普及率惊人,是事实上的标准。
- 在项目中经常会需要应用弹出一些自定义的窗口,这时候Android自带的系统Dialog就无法满足我们的需求了,为了满足项目需求,我们可以使用
- 本文实例讲述了Android实现彩信附件的添加与删除功能。分享给大家供大家参考,具体如下:添加附件在ComposeMessageActivi
- Spring Boot中的那些Conditionalspring boot中为我们提供了丰富的Conditional来让我们得以非常方便的在
- 前言属于基础的面试问题,一定要能够回答全哦~一、继承Thread,重写run方法通过自定义一个类(这里起名为:MyThread),继承Thr
- 本文实例讲述了Android编程实现获取当前连接wifi名字的方法。分享给大家供大家参考,具体如下:WifiManager wifiMgr
- 使用OptionMenu只要重写两个方法public boolean onCreateOptionsMenu(Menu menu):菜单的初
- JNDI的理解JNDI是 Java 命名与文件夹接口(Java Naming and Directory Interface),在J2EE规
- 前提:windows上安装jdk1.启动jar脚本@echo offSTART "app" javaw -jar app
- 目录截屏AudioRecord音频采集MediaCodec编码音频数据Rtp发送数据SDP文件配置音频config配置计算方式:vlc测试播
- Mybatis简介MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参
- 题目描述这是 LeetCode 上的 768. 最多能完成排序的块 II ,难度为 困难。Tag : 「贪心」这个问题和&ldquo
- 前言在Android开发中经常会遇到EditText控件,而在App开发过程中、遇到了这样一个问题、那就是Android EditText控
- 本文实例讲述了Java基于余弦方法实现的计算相似度算法。分享给大家供大家参考,具体如下:(1)余弦相似性通过测量两个向量之间的角的余弦值来度
- 1.概述Spring Boot Admin是一个Web应用程序,用于管理和监视Spring Boot应用程序。每个应用程序都被视为客户端,并
- 前言众所周知,随着Google I/O大会的召开,Google宣布将支持Kotlin作为Android的开发语言,最近几日,关于Kotlin