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.

We can use two trappers 'slow' and 'fast' to help us to find the answer.

Each time, 'slow' will jump to the next node and 'fast' will jump to the next two nodes.

When 'fast' or 'fast.node' reaches the end node, 'slow' reaches the answer.

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow=fast=head
while fast and fast.next:
slow=slow.next
fast=fast.next.next
return slow

  

[LeetCode&Python] Problem 876. Middle of the Linked List的更多相关文章

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

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

  2. 876. Middle of the Linked List - LeetCode

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

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

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

  4. [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. If t ...

  5. 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. ...

  6. [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 ...

  7. 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 ...

  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. [LeetCode&Python] Problem 237. Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

随机推荐

  1. 《剑指offer》第三十二题(分行从上到下打印二叉树)

    // 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...

  2. 【Golang】 可以自动生成测试用例的库--gotests

    简介 gotests是一个Golang命令行工具,它可以使编写Go的测试代码变得容易.它能基于目标源文件的函数和方法生成数据驱动测试用例,并且在此过程会自动导入任何依赖. 下面是gotests在使用S ...

  3. YII第三步,日志开启

    YII第三步,日志开启 index.php入口文件配置: defined('YII_DEBUG') or define('YII_DEBUG',true); cofig/main.php 'prelo ...

  4. 合并两个dt

    C#代码中实现两个表(DataTable)的关联查询(JOIN)   之前通常都是使用SQL直接从数据库中取出表1和表2关联查询后的数据,只需要用一个JOIN就可以了,非常方便.近日遇到一种情况,两个 ...

  5. [.NET开发] C# 如何创建Excel多级分组

    要设置显示或者隐藏分类数据下的详细信息,在便于数据查看.管理的同时也使文档更具美观性.那么,在C#中如何来创建Excel数据的多级分组显示呢?下面将进行详细阐述.方法中使用了免费版组件Free Spi ...

  6. CSS#Flex-box, border-size, onresize() event, Media Queries

    Flexbox Pseudo-classes box-sizing: border-box HTML DOM event  resize() @media Queries: 根据一些css条件,触发一 ...

  7. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  8. Oracle 11g dataguard check real time apply

    2017年8月24日 16:38 环境:oracle 11.2.0.1 OEL-5.8 注:以下操作都在备库执行 总结方法: 1.FPYJ(125_7)@fpyj123> select open ...

  9. store procedure

    store procedure: _________________________________________________ set ANSI_NULLS ON set QUOTED_IDEN ...

  10. 在oaf中集成SpringLoaded实现热部署

    首先声明:其实JRebel和Spring-Loaded就是一个开发环境下的利器,skip build and redeploy process,大大提升了工作效率!而非生产环境的利器... 不要在生产 ...