当前位置:网站首页>力扣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]
边栏推荐
- Codeforces Round #643 (Div. 2)——B. Young Explorers
- Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
- Partage d'expériences de contribution à distance
- Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
- WPF 自定义 写实风 雷达图控件
- Keras' deep learning practice -- gender classification based on inception V3
- mysql/mariadb怎样生成core文件
- JVM memory and garbage collection -4-string
- SQLite3 data storage location created by Android
- Applet running under the framework of fluent 3.0
猜你喜欢
Partage d'expériences de contribution à distance
SQLite3 data storage location created by Android
C language - modularization -clion (static library, dynamic library) use
软件测试笔试题你会吗?
Nacos microservice gateway component +swagger2 interface generation
Matlab r2021b installing libsvm
快手小程序担保支付php源码封装
Keras深度学习实战——基于Inception v3实现性别分类
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
JVM memory and garbage collection-3-runtime data area / heap area
随机推荐
Introduction to grpc for cloud native application development
Exit of processes and threads
C language - modularization -clion (static library, dynamic library) use
Redission源码解析
微软 AD 超基础入门
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
Version 2.0 of tapdata, the open source live data platform, has been released
The method of using thread in PowerBuilder
Mouse event - event object
Introduction to Microsoft ad super Foundation
leetcode 866. Prime Palindrome | 866. 回文素数
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
Redux usage
cv2读取视频-并保存图像或视频
【SolidWorks】修改工程图格式
Matlab r2021b installing libsvm
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
PHP 计算个人所得税
阿南的判断