当前位置:网站首页>力扣5_876. 链表的中间结点
力扣5_876. 链表的中间结点
2022-07-08 00:39:00 【上课不要睡觉了】
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例 1:
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
提示:
给定链表的结点数介于 1 和 100 之间。
来源:力扣(LeetCode)
Java解法1
这里使用了类似C语言里面的双指针的方法,快指针每次移动二格,慢指针每次移动一格最终当快指针走完时正好慢指针走完一半。
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
Java解法2
class Solution {
public ListNode middleNode(ListNode head) {
ListNode[] A = new ListNode[100];
int t = 0;
while (head != null) {
A[t++] = head;
head = head.next;
}
return A[t / 2];
}
}
Python解法
这里的Python采用的是传统方法,先将链表存入列表中,通过索引取得列表的中间元素。
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
A = [head]
while A[-1].next:
A.append(A[-1].next)
return A[len(A) // 2]
边栏推荐
- What are the types of system tests? Let me introduce them to you
- VIM string substitution
- COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
- Remote Sensing投稿經驗分享
- 云原生应用开发之 gRPC 入门
- MySQL查询为什么没走索引?这篇文章带你全面解析
- 【目标跟踪】|atom
- 系统测试的类型有哪些,我给你介绍
- ClickHouse原理解析与应用实践》读书笔记(8)
- JVM memory and garbage collection -4-string
猜你喜欢

In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!

A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

burpsuite

Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"

MySQL查询为什么没走索引?这篇文章带你全面解析

Exit of processes and threads

Sword finger offer II 041 Average value of sliding window

日志特征选择汇总(基于天池比赛)

Nmap tool introduction and common commands
随机推荐
mysql/mariadb怎样生成core文件
Sword finger offer II 041 Average value of sliding window
nmap工具介绍及常用命令
cv2读取视频-并保存图像或视频
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
Matlab r2021b installing libsvm
【SolidWorks】修改工程图格式
Voice of users | understanding of gbase 8A database learning
日志特征选择汇总(基于天池比赛)
See how names are added to namespace STD from cmath file
什么样的MES系统才是好系统
Version 2.0 de tapdata, Open Source Live Data Platform est maintenant disponible
Redisson分布式锁解锁异常
Redisson distributed lock unlocking exception
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
JVM memory and garbage collection-3-object instantiation and memory layout
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
C语言-Cmake-CMakeLists.txt教程