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 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
1and100.
想法:通过两个指针,一个称为快指针,一个称为慢指针,慢指针更新时指向它的下一个节点,而快指针更新时指向它的下下个节点,当快指针到达链表尾端时,慢指针刚好指向链表中间的位置。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode* fast = head;
struct ListNode* slow = head;
while(fast && fast->next){
fast = fast->next->next;
slow = slow ->next;
}
return slow;
}
leetcode-876 Middle of the Linked List的更多相关文章
- [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 876. Middle of the Linked List(获得链表中心结点)
题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...
- 876. Middle of the Linked List - LeetCode
Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...
- 【Leetcode_easy】876. Middle of the Linked List
problem 876. Middle of the Linked List 参考 1. Leetcode_easy_876. Middle of the Linked List; 完
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- [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
1. 原始题目 Given a non-empty, singly linked list with head node head, return a middle node of linked li ...
- 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 ...
随机推荐
- mongodb远程连接访问
随着云计算,云服务的不断发展演进,数据库的管理及维护方式也在转变,传统基于C/S客户端工具管理的方式,已经无法满足实际需要. TreeSoft数据库管理系统,采用web方式,对mongoDB,MySQ ...
- Could not initialize plugin: interface org.mockito.plugins.MockMaker
IDE:Idea 添加依赖 <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte ...
- [转]sqlserver转换为Mysql工具使用
https://files.cnblogs.com/files/miantiaoandrew/mss2sql_v5-3.zip 1.首先下载工具,链接如上 2.解压出来,运行mss2sql.exe 3 ...
- JFinal -基于Java 语言的MVC极速 web 开发框架
JFinal概述 JFinal 是基于Java 语言的极速 web 开发框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful.在拥有Java语言所有优势的同时再 ...
- pycharm 光标快速移动到括号外或行尾
coupon = models.ForeignKey("Coupon", on_delete=models.CASCADE) pycharm中编写程序, 需要将光标快速移动到“” ...
- pgAdmin4 ubuntu python 安装
ubuntu安装pgAdmin4,通过python的pip 安装 pgAdmin4.(首更时间20161205) 新版本的pgAdmin4目前支持mac/window/linux/python,可是l ...
- BCG库的一个bug
作者:朱金灿 来源:http://blog.csdn.net/clever101 同事在使用BCG库图表组件(BCG库的版本为BCGCBPRO1510)时遇到一个问题,就是图表标题总是出现乱码, ...
- SQLServer 学习笔记之超详细基础SQL语句 Part 1
Sqlserver 学习笔记 by:授客 QQ:1033553122 1创建数据库 格式: CREATE DATABASE database_name ON PRIMARY(在组文件组中指定文件) ( ...
- python2编码的问题
1,python2的默认编码是ascii码. 2,python2中有2中数据模型来支持字符串这种数据类型,分别为str和unicode. 3,uncode转换为其他编码是encode,其他编码转换成u ...
- jqGrid删除单条和多条数据
$("#Delete").click(function () { //var id = $("#showGrid").jqGrid('getGridParam' ...