[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 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
and100
.
思路直接求出来linked list的长度, 然后再走一半, 返回head即可.
Improve, 可以用slow 和fast 去走, 当fast走到头, 那么slow就在中间了.
Code T: O(n) S: O(1)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def middleNode(self, head):
def helper(head):
count = 0
while head:
count += 1
head = head.next
return count for i in range(helper(head)//2):
head = head.next
return head
[LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers的更多相关文章
- [LeetCode] 206. Reverse Linked List_Easy tag: Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] 83. Remove Duplicates from Sorted List_Easy tag: Linked List
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 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. 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(获得链表中心结点)
题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
随机推荐
- 关于JS获取某月最后一天
发现网上用js获取某月最后一个的方式大多比较复杂,上个简单的: new Date(2013,4).toJSON().substring(0,10) new Date(2013,4,0).toLocal ...
- CRUD的操作,增删改查!
.注释语法:--,# .后缀是.sql的文件是数据库查询文件 .在创建查询里,那个需要保存的对话框只是,保存查询. .在数据库里面 列有个名字叫字段 行有个名字叫记录 CRUD操作: create 创 ...
- python 给字符串加颜色
msg = '\033[41;1m字符串内容\033[0m' print(msg) # \033[41;1m起始位置 改变41数值就是改变其他颜色,.033[0m 结束位置
- CCPC-Wannafly Winter Camp Day3 Div1 - 石头剪刀布 - [带权并查集]
题目链接:https://zhixincode.com/contest/14/problem/I?problem_id=211 样例输入 1 3 5 2 1 1 2 1 2 1 1 2 3 2 1 ...
- CCPC-Wannafly Winter Camp Day4 Div1 - 置置置换 - [DP]
题目链接:https://zhixincode.com/contest/18/problem/G?problem_id=265 题目描述 wls有一个整数 $n$,他想请你算一下有多少 $1...n$ ...
- [No0000115]打开Excel2016提示内存或磁盘空间不足的解决方法
症状: 法一:右键文件,并 解除锁定: 法二: 在系统的服务中查看Windows Firewall服务 和Windows Update服务是否开启,如果没有开启就把他们启动一下. 1.在桌面的[计算机 ...
- shell之使用cut切割文本文件
我们知道可以通过工具grep或egrep按行筛选记录,这里我们可以通过cut工具对文本按列进行切分,它可以指定定界符,linux下制表符是默认的定界符. #cut -f 2,3 textfile 这个 ...
- arcengine右键实现new group layer的功能
没有找到相关方法,但是有对图层组进行操作的资料. https://gis.stackexchange.com/questions/43620/how-do-i-reach-a-layer-inside ...
- HBase实战 | 知乎实时数仓架构演进
https://mp.weixin.qq.com/s/hx-q13QteNvtXRpNsE5Y0A 作者 | 知乎数据工程团队编辑 | VincentAI 前线导读:“数据智能” (Data Inte ...
- distributed computing_the World Wide Web
RESTful Web APIs_2013 I'm going to show you a better way to do distributed computing, using the idea ...