Lintcode228-Middle of Linked List-Naive
228. Middle of Linked List
Find the middle node of a linked list.
Example
Example 1:
Input: 1->2->3
Output: 2
Explanation: return the value of the middle node.
Example 2:
Input: 1->2
Output: 1
Explanation: If the length of list is even return the value of center left one.
Challenge
If the linked list is in a data stream, can you find the middle without iterating the linked list again?
看着简单,但需要思路缜密。
考虑两种情况:
case 1: 空链表 (要确保head不是一个空结点,否则, while(head.next) 就会抛出空指针异常! 记住:写head.next之前一定要排除空结点的情况!!!!!!!!!!
case 2: 链表不为空 (快慢指针)
快慢指针同时指向头结点,慢指针(result)走一步,快指针(head)走两步。
1)如果链表有偶数个结点,那么,当快指针 head.next.next == null 时 (事件A)
2)如果链表有奇数个结点,那么,当快指针 head.next == null 时 (事件B)
返回慢指针指向的结点(事件C)
当上面两种情况都不发生时,慢指针(result)走一步,快指针(head)走两步。(Not C)
即:C = A || B, 那么 Not C = !A && !B
WARNING!
while (head.next != null && head.next.next != null) 不能写成 while (head.next.next != null && head.next != null)
因为如果是奇数结点的链表(1->2->3->null)当慢指针指向2时,快指针指向3。下一次循环如果先判断head.next.next 就会抛出空指针异常,所以应该先检验head.next是否为空,不满足(就不会执行第二个布尔表达式)直接退出循环。
代码:
public ListNode middleNode(ListNode head) {
ListNode result = head;
if (head == null) {
return result;
}
while (head.next != null && head.next.next != null) {
result = result.next;
head = head.next.next;
}
return result;
}
Lintcode228-Middle of Linked List-Naive的更多相关文章
- Solutions and Summay for Linked List Naive and Easy Questions
1.Remove Linked List Elements package linkedlist; /* * Question: Remove all elements from a linked l ...
- Middle of Linked List
Find the middle node of a linked list. Example Given 1->2->3, return the node with value 2. Gi ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- linkedlist--lecture-4
1.链表数据结构 内存利用率高:动态分配 2.链表类定义 单向链表节点 public calss ListNode { int val =0; ListNode next = null; public ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
- [Swift]LeetCode876. 链表的中间结点 | 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 ...
- 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. ...
随机推荐
- 浅谈最长上升子序列(LIS)
一.瞎扯的内容 给一个长度为n的序列,求它的最长上升子序列(LIS) 简单的dp n=read(); ;i<=n;i++) a[i]=read(); ;i<=n;i++) ;j<i; ...
- 友金所招聘Java工程师面试题
友金所是一家位于深圳南山科技园的P2P网贷公司,用友集团控股.该公司采用渐进式的问答面试,没有笔试题.比如面试者说熟悉多线程,面试官就问哪些方式解决并发:面试者说采用Synchronized或者Ree ...
- JavaWeb学习之三层架构实例(三)
引言 通过上一篇博客JavaWeb学习之三层架构实例(二)我们基本上已经实现了对学生信息列表的增删改查操作(UI除外),但是不难看出,代码冗余度太高了,尤其是StudentDao这个类,其中的增删改查 ...
- NABCD分析---校园服务
N(需求): 大学生活中,很多琐碎的小事浪费同学时间精力.我们的APP本着为同学服务的宗旨,解决生活中各方面的问题,同学们可以在APP上发布各种信息,例如兼职,二手买卖等等. A(做法): 用户打开A ...
- Linux学习路线全解,Linux操作系统学习路线
大家都知道,在现在这个信息化飞速发展的时代,IT技术火速发展,信息的重要性,可想而知.现在,在北京当一个高级运维工程师,年薪百万已经不是梦想.当然我也想,谁不想挣大钱,开好车,住好房.下面说说自己的一 ...
- 获取Button脚本挂载的事件名
(function(){ var Super = function(){}; Super.prototype = cc.Button.prototype; //实例化原型 Super.prototyp ...
- 66.ajax--ajax请求多个url解决办法
ajax请求多个url解决办法 以下四种方法是我找的,我也进行实践过. 测试中有四个请求接口,原本需要13S,用了第三种方法缩减到7S,但是仍不能达到2S以内. 所以仅供参考,待我找到能缩减到2S以内 ...
- 00JAVA语法基础_动手动脑
1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本类.枚举目的就是要让某个变量的取值只能为若干固定值中的 ...
- 《linux就该这么学》第十七节课:第18,19,23章,mariadb数据库、PXE无人值守安装系统和openldap目录服务。
第23章 (借鉴请改动) openldap数据的特点:1.短小.2.读取次数较多 上述说明: openLDAP服务端配置: 1.yum install -y openldap openldap ...
- eclipse jpa 工具生成实体类
1,配置数据库连接 打开eclipse, 选择数据库: 下一步,选择驱动包 根据自己的驱动包路径选择对应的驱动包: 2,配置jpa-tool 在项目上面右键->properties 点击ok,然 ...