876. Middle of the Linked List
1. 原始题目
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.
2. 题目理解
就是找到中间结点,然后依次打印链表结点。方法就是利用两个快慢指针。从头开始,如果快指针每次走两步,慢指针每次走一步,那么当快指针走到末尾时,慢指针正好在中间位置。
3. 解题
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
pre = head # 快指针
cur = head # 慢指针
while(pre and pre.next):
cur = cur.next
pre = pre.next.next return cur
检验:
# 新建链表1
listnode1 = ListNode_handle(None)
#s1 = [1,2,3,666,8,3,2,9,4,5,6,8,999,666]
s1 = [1,3,5,666,4,5]
for i in s1:
listnode1.add(i)
listnode1.print_node(listnode1.head) s = Solution()
head = s.middleNode(listnode1.head)
listnode1.print_node(head)
1 3 5 666 4 5
666 4 5
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 链表的中间结点
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 ...
- [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 ...
- 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】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- LeetCode 876. Middle of the Linked List(获得链表中心结点)
题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...
随机推荐
- 洛谷P1073 Tarjan + 拓扑排序 // 构造分层图
https://www.luogu.org/problemnew/show/P1073 C国有 n n个大城市和 mm 条道路,每条道路连接这 nn个城市中的某两个城市.任意两个城市之间最多只有一条道 ...
- go etcd
etcd介绍 GitHub:https://github.com/coreos/etcd 官网:https://coreos.com/etcd/ 下载:https://github.com/coreo ...
- python 微信爬虫实例
单线程版: import urllib.request import urllib.parse import urllib.error import re,time headers = (" ...
- float clearfix
Float float 属性的原本作用是: 为了实现文字环绕效果 float 父元素高度塌陷实现文字环绕效果 float 固定一列宽的自适应布局 float 多列布局` float 固定一列宽的自适应 ...
- Kafka安装部署
1.从官网下载安装包,并通过Xftp5上传到机器集群上 下载kafka_2.11-1.1.0.tgz版本,并通过Xftp5上传到hadoop机器集群的第一个节点node1上的/opt/uploa ...
- 在CentOS7上安装Zabbix3.0
#!/bin/bash # # .配置无人值守的安装,定义安装过程中需要用到的一些信息 # mysql_root_pw=root_pw mysql_zabbix_pw=zabbix_pw DBPass ...
- tedu训练营day03
Day03笔记1.作业 1.假如你现在25周岁,每年365天,计算你过了多少个星期天(大概数字) 提示 :地板除 2.毕业薪资为10000元,每年涨20%,十年之后你的薪资为多少元? 提示: 幂运算( ...
- python初认识、基础数据类型以及 if 流程控制
python初认识 CPU.内存.硬盘以及操作系统之间的关系 CPU:中央处理器,计算机的逻辑运算单元 硬盘:长期存储数据的地方,断电不会丢失 内存:位于CPU与硬盘之间,缓解高速CPU与低速硬盘之间 ...
- 5年GTD自我管理经验,一块听听
我在胜利油田做了20多年的油田信息化工作,以前的我经常处于这样一种状态: 当我正在做着手边的一项事情时,头脑里却不断地蹦出来其它的事务,让我烦心不已,焦虑不安:PPT经常要拖到汇报当天的凌晨才做完,有 ...
- 关于Unicode
http://www.unicode.org/faq/utf_bom.html 有关UTF或编码表单的一般问题 Unicode是16位编码吗? Unicode文本可以以多种方式表示吗? 什么是UTF? ...