当前位置:网站首页>力扣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]
边栏推荐
- Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
- 快手小程序担保支付php源码封装
- [target tracking] |atom
- [recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
- 【目标跟踪】|atom
- Can you write the software test questions?
- nmap工具介紹及常用命令
- Applet running under the framework of fluent 3.0
猜你喜欢

Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension

Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)

The function of carbon brush slip ring in generator

How to fix the slip ring

C language - modularization -clion (static library, dynamic library) use

数据链路层及网络层协议要点

神经网络与深度学习-5- 感知机-PyTorch

How to make the conductive slip ring signal better

Reading notes of Clickhouse principle analysis and Application Practice (7)

metasploit
随机推荐
2022国内十大工业级三维视觉引导企业一览
JVM memory and garbage collection-3-runtime data area / method area
Version 2.0 of tapdata, the open source live data platform, has been released
Remote Sensing投稿經驗分享
[error] error loading H5 weight attributeerror: 'STR' object has no attribute 'decode‘
C语言-Cmake-CMakeLists.txt教程
Redisson distributed lock unlocking exception
The circuit is shown in the figure, r1=2k Ω, r2=2k Ω, r3=4k Ω, rf=4k Ω. Find the expression of the relationship between output and input.
WPF custom realistic wind radar chart control
Remote Sensing投稿经验分享
Reading notes of Clickhouse principle analysis and Application Practice (7)
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
谈谈 SAP 系统的权限管控和事务记录功能的实现
Installing and using mpi4py
Sum of submatrix
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
QT -- create QT program
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
VIM use