当前位置:网站首页>876. 链表的中间结点
876. 链表的中间结点
2022-07-04 17:39:00 【charlsdm】
- 链表的中间结点
给定一个头结点为 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 之间。
下面附上我的AC代码
public partial class Solution
{
public ListNode MiddleNode(ListNode head)
{
ListNode[] listNodes = new ListNode[100];
int index = 0;
while(head!=null)
{
listNodes[index] = head;
head = head.next;
index = index + 1;
}
return listNodes[(index)/2];
}
}
边栏推荐
猜你喜欢
Scala basic tutorial -- 17 -- Collection
Journal des problèmes de brosse à boutons de force / day6 / 6.28
2022 ByteDance daily practice experience (Tiktok)
Basic tutorial of scala -- 16 -- generics
An example of multi module collaboration based on NCF
Principle and application of ThreadLocal
Li Kou brush question diary /day7/2022.6.29
1、 Introduction to C language
一、C语言入门基础
Scala基础教程--15--递归
随机推荐
Scala基础教程--12--读写数据
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
神经网络物联网平台搭建(物联网平台搭建实战教程)
One question per day (2022-07-02) - Minimum refueling times
Caché JSON 使用JSON适配器
My colleagues quietly told me that flying Book notification can still play like this
Scala basic tutorial -- 15 -- recursion
Cache é JSON uses JSON adapters
【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
[opencv introduction to mastery 9] opencv video capture, image and video conversion
Other InterSystems%net tools
性能优化之关键渲染路径
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Halcon模板匹配
删除字符串中出现次数最少的字符【JS,Map排序,正则】
Li Chi's work and life summary in June 2022
php伪原创api对接方法
Li Kou brush question diary /day1/2022.6.23
Li Kou brush question diary /day7/6.30
Li Kou brush question diary /day2/2022.6.24