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的更多相关文章

  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 链表的中间结点

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

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

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

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

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

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

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

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

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

随机推荐

  1. python对象初始化

    当python对象被创建以后,需要将对象进行初始化.Python有一个构造函数和一个初始化函数: 1.构造函数__new__,只接受一个参数,即类本身(它会在对象被构造之前调用,所以这里也就没有sel ...

  2. consul介绍

    consul 是一个支持多数据中心分布式高可用,用于服务发现和配置共享的工具. consul与其它工具的不同,官方介绍如下: https://www.consul.io/intro/vs/index. ...

  3. hive metastore && hiveserver2 . jvm 配置调整优化

    hive-env.sh 添加如下,其中踩坑踩了不少. if [ "$SERVICE" = "metastore" ]; then if [ -z "$ ...

  4. Docker笔记一:Docker介绍

    目录 什么是Docker? Docker的核心概念 Docker镜像命令 Docker容器命令 Docker实战 查看我的镜像 启动Redis Docker中国镜像加速 血与泪的教训 什么是Docke ...

  5. Hadoop问题:java.net.SocketException: Network is unreachable

    问题描述:Failed on local exception: java.net.SocketException: Network is unreachable; Host Details : loc ...

  6. CentOS7 下 Hadoop 分布式部署

    Hadoop 服务划分 使用三台节点,集群部署规划如下 服务\主机 hadoop1 hadoop2 hadoop3 HDFS NameNode DataNode DataNode SecondaryN ...

  7. python 包和模块间的引入

    ##############################总结####################### 主要内容: 1. 模块 2. import 3. from xxx import xxx ...

  8. Ubuntu 启动服务 失败 的 可能解决办法

    ➜  ~ service nfs-kernel-server start  Job for nfs-server.service canceled. 如果遇到服务启动失败,请检查有关的文件夹是否创建( ...

  9. lasticsearch最佳实践之分片使用优化

    一.遇到的问题 与大多数分布式系统一样,Elasticsearch按照一定的Hash规则把用户数据切分成多个分片,然后打散到不同机器进行存储,从而实现大规模数据的分布式存储. cluster.png ...

  10. css3实现单行文本溢出显示省略号

    文本超出一定宽度让其隐藏,以省略号替代 width:200px; white-space:nowrap; text-overflow:ellipsis; overflow:hidden; 如下图