华为OD机试之单向链表中间节点(Java源码)_华为od机试 - 单向链表中间节点(2)

输入描述

  1. 第一行 链表头节点地址 后续输入的节点数n
  2. 后续输入每行表示一个节点,格式 节点地址 节点值 下一个节点地址(-1表示空指针)
  3. 输入保证链表不会出现环,并且可能存在一些节点不属于链表。

输出描述

单向链表中间的节点值

源码和解析
解析:

  1. 这种题目相对来说较好理解,可以简单理解为将节点按首尾相接的形式排序,首先第一行指定了节点开始地址。后续每行输入节点开始地址,值和结束地址。
  2. 输入时,可以使用循环来接收每个节点的信息。并装入一个ArrayList之中。当接收的节点开始地址等于第一行的开始地址时,证明该节点就是链表的第一个节点。
  3. 编写一个方法,用于取出未排序ArrayList中的目标节点。也即是有序链表的下一个节点。
  4. 这里需要注意的是,取出节点后就将目标节点从原始列表中移出,这样可以减少下一次查找的时间。如果在列表中无法找到目标节点,那么其他节点就不需要管了。我们只需要以开始地址为开头的那个节点形成链表的中间值。其他不在链表之中的就过滤掉即可。

示例代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class T24 {
	static class Point{
		String start;//开始地址
		int value;//值
		String next; //下一个节点地址
		@Override
		public String toString() {
			return "Point [start=" + start + ", value=" + value + ", next="
					+ next + "]";
		}
	}
	public static void main(String[] args) {
		System.out.println("请输入:");
		Scanner scanner=new Scanner(System.in);
		String input=scanner.nextLine();
		Point startPoint=new Point();
		int number=Integer.parseInt(input.split(" ")[1]);
		startPoint.start=input.split(" ")[0];
		List<Point> rawPoints=new ArrayList<T24.Point>();
		//排序后的List
		List<Point> orderList=new ArrayList<>();
		for(int i=0;i<number;i++){
			input=scanner.nextLine();
			Point point=new Point();
			point.start=input.split(" ")[0];
			point.value=Integer.parseInt(input.split(" ")[1]);
			point.next=input.split(" ")[2];
			if(point.start.equals(startPoint.start)){
				startPoint=point;
				orderList.add(point);
			}else{
				rawPoints.add(point);
			}
		}


# Docker步步实践

**目录文档:**

![](https://img-blog.csdnimg.cn/img_convert/9fd27e41264121553a1aeb827fcb629a.webp?x-oss-process=image/format,png)

![](https://img-blog.csdnimg.cn/img_convert/211bcdc4c2d5a9d4cd40c8ba8f4c8f98.webp?x-oss-process=image/format,png)

**①Docker简介**

**②基本概念**

**③安装Docker**

![](https://img-blog.csdnimg.cn/img_convert/5606d6cd1f2a46480b48c376e88f03a7.webp?x-oss-process=image/format,png)

**④使用镜像:**

![](https://img-blog.csdnimg.cn/img_convert/cf4bae9a8a645dfd58704735e803bcb8.webp?x-oss-process=image/format,png)

**⑤操作容器:**

![](https://img-blog.csdnimg.cn/img_convert/e929c4ea3875d0aaa0fd88af2c0da227.webp?x-oss-process=image/format,png)

**⑥访问仓库:**

![](https://img-blog.csdnimg.cn/img_convert/1590d2e1f92c619be722ca2f584045af.webp?x-oss-process=image/format,png)

**⑦数据管理:**

![](https://img-blog.csdnimg.cn/img_convert/32f23b61f44449050ef5128a332ebad9.webp?x-oss-process=image/format,png)

**⑧使用网络:**

![](https://img-blog.csdnimg.cn/img_convert/e0696bfc5b807bea0df8495892183e18.webp?x-oss-process=image/format,png)

**⑨高级网络配置:**

![](https://img-blog.csdnimg.cn/img_convert/08694984844dde70d2d3cd4677eb92d1.webp?x-oss-process=image/format,png)

**⑩安全:**

![](https://img-blog.csdnimg.cn/img_convert/e9c410b445e9f3f1290d4e57b95a7805.webp?x-oss-process=image/format,png)

**⑪底层实现:**

![](https://img-blog.csdnimg.cn/img_convert/d58858fb2c71bd9dd2901629ea4f700c.webp?x-oss-process=image/format,png)

**⑫其他项目:**

![](https://img-blog.csdnimg.cn/img_convert/94f64dfcde47e897d8a533cb5c4d6398.webp?x-oss-process=image/format,png)

hC0X8-1714636849324)]

**⑪底层实现:**

[外链图片转存中...(img-sHdUblY3-1714636849324)]

**⑫其他项目:**

[外链图片转存中...(img-o1erPSgI-1714636849325)]

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/topics/618154847)收录**
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值