Given a non-empty, singly linked list with head node `head`, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

Note:

  • The number of nodes in the given list will be between 1 and 100.

这道题给了一个链表,让我们找其中间结点。由于链表不像数组,不能通过坐标位置来直接访问元素,而是只能从头结点开始,使用 next 指针来访问之后的结点,为了知道当前结点的位置,还得使用计数器来记录。由于在不知道链表的总长度之前,是无法知道中间结点的位置的,那么可以首先遍历一遍,统计出链表的长度,此时长度有了,除以2就是中间结点的位置了,再从头遍历一遍,就可以找出中间结点的位置了,参见代码如下:


解法一:

class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *cur = head;
int cnt = 0;
while (cur) {
++cnt;
cur = cur->next;
}
cnt /= 2;
while (cnt > 0) {
--cnt;
head = head->next;
}
return head;
}
};

由于链表无法通过坐标位置来访问元素,但我们可以将所有的结点按顺序存入到一个数组中,那么之后就可以直接根据坐标位置来访问结点了,参见代码如下:


解法二:

class Solution {
public:
ListNode* middleNode(ListNode* head) {
vector<ListNode*> vec(100);
int cur = 0;
while (head) {
vec[cur++] = head;
head = head->next;
}
return vec[cur / 2];
}
};

上面两种方法一个多用了时间,一个多用了空间,其实都不是最优的解法,最好的方法其实是使用快慢指针来做。在之前那道 [Linked List Cycle](https://www.cnblogs.com/grandyang/p/4137187.html) 链表中找环的题,我们介绍过快慢指针,就是两个指针,慢指针一次走一步,快指针一次走两步,那么这里当快指针走到末尾的时候,慢指针刚好走到中间,这样就在一次遍历中,且不需要额外空间的情况下解决了问题,参见代码如下:


解法三:

class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *slow = head, *fast = head;
while (head && head->next) {
slow = slow->next;
head = head->next->next;
}
return slow;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/876

类似题目:

Linked List Cycle

参考资料:

https://leetcode.com/problems/middle-of-the-linked-list/

https://leetcode.com/problems/middle-of-the-linked-list/discuss/154619/C%2B%2BJavaPython-Slow-and-Fast-Pointers

https://leetcode.com/problems/middle-of-the-linked-list/discuss/155148/Java-O(n)-time-and-O(1)-space-solution-without-using-fastslow-pointer

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 876. Middle of the Linked List 链表的中间结点的更多相关文章

  1. LeetCode 876 Middle of the Linked List 解题报告

    题目要求 Given a non-empty, singly linked list with head node head, return a middle node of linked list. ...

  2. [LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...

  3. LeetCode 876. Middle of the Linked List(获得链表中心结点)

    题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...

  4. 876. Middle of the Linked List - LeetCode

    Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...

  5. 【Leetcode_easy】876. Middle of the Linked List

    problem 876. Middle of the Linked List 参考 1. Leetcode_easy_876. Middle of the Linked List; 完

  6. 【LeetCode】876. Middle of the Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...

  7. [LeetCode&Python] Problem 876. Middle of the Linked List

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...

  8. 876. Middle of the Linked List【Easy】【单链表中点】

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...

  9. 876. Middle of the Linked List

    1. 原始题目 Given a non-empty, singly linked list with head node head, return a middle node of linked li ...

随机推荐

  1. Debug 路漫漫-15:Python: NameError:name 'dataset' is not defined

    在调试 <Outer Product-based Neural Collaborative Filtering>论文的源码(https://github.com/duxy-me/ConvN ...

  2. mybatis错误:There is no getter for property named 'delegate' in 'class com.sun.proxy.$Proxy某某'

    错误描述: There is no getter for property named 'delegate' in 'class com.sun.proxy.$Proxy32' 错误原因: 1.你有多 ...

  3. servlet中的IllegalStateException

    IllegalStateException在java web开发中比较常见,IllegalStateException的根本原因是java servlet在提交响应后,还尝试写内容. 所以避免Ille ...

  4. 原生javascript 共享onload事件

    在工作时,我们给一个元素绑定了事件,如果dom还没加载完成,就执行了js代码,就不会绑定成功.常规解决方案就是用: window.onload = EventFunction; 可是如果有两个 事件, ...

  5. vue-cli安装以及创建一个简单的项目(二)(vuex使用、发行一个简单的app)

    1.vuex的使用 vuex是vue的状态管理中心,vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,常用于: 1.多个视图依赖同一状态(l例:菜单导航) 2.来自不同 ...

  6. C# 之扩展方法

    在编程过程中,有时由于新的需求,可能就会需要对类型进行修改,但当需要为类型添加新功能但并不拥有类型的已有代码时,就需要用到 扩展方法; 使用扩展方法的方式:创建一个新的类,这个类必须是静态类. 在这个 ...

  7. Web Api全局预防Xss攻击

    本文转载自https://www.cnblogs.com/ruanyifeng/p/4739807.html.对第二种过滤方法的代码进行了一些修改和注释,记录一下免得以后忘了.已经测试过,应该可以直接 ...

  8. 打印X

    ***.....***//    .***...***.//    ..***.***..//    ...*****...//    ....***....//    ...*****...//   ...

  9. WPF 基础总结(学习建议)

    举个简单得例子, 类似造房子, 当然实际上可能非常细, 对应的如下所示: 在此之前, 需要了解的是. WPF项目是怎么启动的 Xaml的结构是怎么样组成, 命名控件定义引用的方法. 知道了如何在Xam ...

  10. MySQL基础(一)(启动/停止、登录/退出、语法规范及最基础操作)

    1.启动/停止MySQL服务 启动:net start mysql    停止:net stop mysql 2.MySQL登录/退出 登录:mysql 参数:如果连接的是本地服务器,一般用命令:my ...