[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 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
1and100.
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的更多相关文章
- 【Leetcode_easy】876. Middle of the Linked List
problem 876. Middle of the Linked List 参考 1. Leetcode_easy_876. Middle of the Linked List; 完
- 876. Middle of the Linked List - LeetCode
Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- [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 ...
- 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. ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- lua_VC6环境
1. 下载得到 lua-5.1.4.tar.gz,解压得到 文件夹"lua-5.1.4" 2. 视频[02:00] 将 lua-5.1.4/etc/luavs.bat 复制到 lu ...
- Python - os 文件/目录操作
最近经常用到 os 操作文件/目录,感觉挺好使的,但是一直没有系统的梳理学习一下, 今天想借此机会整理一下工作中常用的方法,也算自己总结学习的一个积累吧. 直接上代码,注释明了 #-*-coding: ...
- PHP使用phpstorm进行断点调试
1.下载xdebug模块 (https://xdebug.org/) 注意:带"ts"是线程安全的意思,"nts"的他没有标示,也就是说,如果是nts的要下载没 ...
- Django MySQL数据库操作
上一篇文章写了一些基本的Django操作,下面重点介绍数据库的内容. 对象之间的关系: 一对一 一对多 多对多 1.一对多 先演示一对多的关系,多个blog对应一个名字, 修改blog/models. ...
- Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)
用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...
- python-day54--前端之js-DOM对象
一.DOM对象 1.什么是HTML DOM HTML Document Object Model(文档对象模型---标签) 2.功能:定义了访问(查找)和操作HTML文档的标准方法 3.HTML ...
- hdu 1069 DAG加权
题目: Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- dp练习(5)——最长严格上升子序列
1576 最长严格上升子序列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给一个数组a1, a2 ... ...
- 简话Angular 03 Angular内置表达式大全
一句话: 大多数html标签属性和事件都有一个对应的ng指令 说明:这些指令和原生html最大的区别就是可以动态更新.比如一个div的样式用ng-class后,我们就可以随意应用css class. ...
- 为何 Delphi的 Local Variables 突然没有值显示了
可能是上次编译后 code未再修改过. 试试 随便 输入一个空格,然后F9