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 { * ...
随机推荐
- 2017-12-15python全栈9期第二天第四节之格式化输出%s和用户交互个人简历模板
#!/user/bin/python# -*- coding:utf-8 -*-name = input('姓名:')age = input('年龄:')job = input('工作:')hobbi ...
- network / switchboard / jiaohuanji
s 步骤1:模拟交换机电源故障,验证设备运行正常 步骤2:模拟交换机主控故障,验证设备运行正常 步骤3:模拟交换机业务单板故障,验证业务运行正常 -- 需要验证业务 步骤4:模拟交换机堆叠分裂 -- ...
- linux中文件多行合并为一行的例子
现网中经常遇到匹配到某一关键字下的所有行合并到同一行,再次匹配到相关关键字再和下面的合并,示例如下: # line1ab# line2cde# line3f想要变成: # line1 a b# lin ...
- Jquery 添加插件
原文:http://www.iteye.com/topic/545971 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法 ...
- Eclipse创建SpringMVC,Spring, Hibernate项目
创建一个java project,创建连个folder,分别命名为java和webapp,删除src文件夹. 打开工程的propertise设置中的build to path,将输出设置为工程名\we ...
- tedu训练营day01
1.三大操作系统 1.Unix :MacOS 2.Linux :Ubuntu18.04 .CentOS.RedHat 3.Windows :Win7.Win8.Win102.VMware Workst ...
- JS 数组中对象去重 reduce 用法
对于数组对象,传统的去重方法无能为力,至于forEach().filter()等迭代方法也不好使:真正能做到优雅去重的,是ES5新增加的一个方法——reduce() 高手给的,完美方法 let log ...
- 添加Glide图片加载框架依赖
1.添加依赖implementation 'com.github.bumptech.glide:glide:4.7.1' 2.放置一个ImageView.3.加载,ivGif是ImageView实例 ...
- GCC编译器原理(一)03------GCC 工具:gprof、ld、libbfd、libiberty 和libopcodes
1.3.7 gprof:性能分析工具 参考文档:https://www.cnblogs.com/andashu/p/6378000.html gprof是GNU profile工具,可以运行于linu ...
- C# MVC EF框架 用事务
using System.Transactions; [HttpPost] public JsonResult Update(InfoModel list) { using (TransactionS ...