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 ...
随机推荐
- spring-bean实例化三种方式
在spring中,bean的示例化有三种方式. 1.使用类的无参构造函数创建 2.使用静态工厂方式创建 3.使用实例化工厂方式创建. 具体代码如下 静态工厂方式: Bean2.java package ...
- POJ1269(KB13-D 计算几何)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16681 Accepted: 71 ...
- 使用PeopleSoft 异步操作设置步骤
在手机端调PS接口或同步数据到中间库等程序中经常用到异步操作. 在主菜单>PeopleTools>集成代理程序>集成设置中添加了服务和服务操作后需要设置以下3步才能完成异步程序: 1 ...
- cent7安装ffmpeg
FFmpeg是一个领先的多媒体框架工具集,几乎能够对任何格式的音视频文件进行解码,编码,转码,复用,解复用,流式传输,过滤和播放. 不管这些音视频文件所采用的格式是由某些标准委员会,社区还是公司设计的 ...
- jbosscache
JBossCache 讲解说明 是什么? 一个树形结构.支持集群.支持事务的缓存技术. 有什么作用? JBoss Cache是针对Java应用的企业级集群解决方案,其目的是通过缓存需要频繁访问的Jav ...
- oracle 使用绑定变量极大的提升性能
初始化操作 SQL> alter system flush shared_pool; SQL> set timing on; 1. 未使用绑定变量的时候代码如下 declare type ...
- jquery实现显示textarea输入字符数
起初会想到使用keyup.keydown.keypress或者是onchange事件,onchange需要失去焦点才触发, 其它三个有些对按住键盘某个键不放不生效,有些对使用中文输入法正在输入时统计不 ...
- 解决MySQL5.6中的Slave延迟问题的基本教程
一.原因分析一般而言,slave相对master延迟较大,其根本原因就是slave上的复制线程没办法真正做到并发.简单说,在master上是并发模式(以InnoDB引擎为主)完成事务提交的,而在sla ...
- 转:35个让人惊讶的 CSS3 动画效果演示
本文收集了35个惊人的 CSS3 动画演示,它们将证明 CSS3 Transform 和 Transition 属性的强大能力.CSS 是网页设计非常重要的一部分,随着越来越多的浏览器对 CSS3 支 ...
- trait代码复用
在面对对象编程中我们经常通过继承来解决部分代码多次出现的问题 php支持单继承,有时候由于不相关联的两个类的方法相同我们需要进行继承操作, trait可以实现不继承的情况下复用代码 trait的使用类 ...