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 { * ...
随机推荐
- GET和POST传输方式
GET和POST传输 在很多人看来,get和post的区别有比如安不安全,传输有大小限制等,在这里,我将对get和post做出客观的评价: GET: 传输方法:get传输数据一般是在地址栏的url的问 ...
- [NIO-1]缓冲区
常用的是ByteBuffer.CharBuffer
- C语言宏定义##连接符和#符的使用
1. 关于宏(Macro) 属于编译器预处理的范畴,属于编译器概念(而非运行期概念). 2. 关于# #的功能:是 将其后面的宏参数进行 字符串化操作(Stringfication),即:在对它所引用 ...
- javascript在计算浮点数(小数)不准确,解决方案
方案来自网络,实现简单,便于做加减乘除使用,由于项目临时要用记录下 如需要更加复杂的计算类库,可以考虑 math.js等知名类库 /** * floatTool 包含加减乘除四个方法,能确保浮点数运算 ...
- JAVA正确地自定义比较对象---如何重写equals方法和hashCode方法
在实际应用中经常会比较两个对象是否相等,比如下面的Address类,它有两个属性:String province 和 String city. public class Address { priva ...
- 完全使用UDP登录Linux
===============Mosh 登录器========================================= == 针对TCP被某些防火墙阻断的Linux机器, 该程序可以让你不使 ...
- Error:Failed to resolve: com.android.support:recyclerview-v7:26.1.0
修改gradle allprojects { repositories { maven { url "https://maven.google.com" } jcenter() } ...
- SQL行列转置
今天给公司同事们出了一道例行考试题,要求写一句SQL语句从上面表转换为下面表,经过艰难思索,一个同事做了出来. 小区 总数 A类车 B类车 C类车建业森林半岛 2 ...
- HttpService
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...
- SSH免费登录
SSH免费登录很简单,如果有用过git的就更简单了 只需要一下两步操作就OK: 1.生成公钥和私钥,在linx上生成公钥和私钥,执行:ssh-keygen 2.执行ssh-copy-id +ip 例如 ...