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的更多相关文章

  1. 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 ...

  2. Middle of Linked List

    Find the middle node of a linked list. Example Given 1->2->3, return the node with value 2. Gi ...

  3. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  4. linkedlist--lecture-4

    1.链表数据结构 内存利用率高:动态分配 2.链表类定义 单向链表节点 public calss ListNode { int val =0; ListNode next = null; public ...

  5. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 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. ...

随机推荐

  1. python 判断字符串是否为(或包含)IP地址

    下面是某个字符串是否为IP地址import re def isIP(str): p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5 ...

  2. 3种检测页面是否符合amp标准的方法

    AMP的关键优势不仅仅在于它能让你的页面更快,还在于它的快可以被验证.有几种方法可以验证AMP文档,它们都会产生完全相同的结果,选择最适合您的开发风格的方法.除了AMP的有效性,您可能还想确认您的AM ...

  3. JMeter接口自动化测试实例—JMeter引用javaScript

    Jmeter提供了JSR223 PreProcessor前置处理器,通过该工具融合了Java 8 Nashorn 脚本引擎,可以执行js脚本以便对脚本进行前置处理.其中比较典型的应用就是通过执行js脚 ...

  4. (转载)js日期格式化转化

    原文地址:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html // 对Date的扩展,将 Date 转化为指定格式 ...

  5. LigerUI之Grid使用详解(一)——显示数据 --分页

    http://www.cnblogs.com/jerehedu/p/4218560.html 首先给大家介绍最常用的数据展示组件Grid,使用步骤如下: 1.页面中正确引入样式文件及相应组件 < ...

  6. Oracle 10.2.0.5升级至11.2.0.4

    参照MOS 官方文档Complete Checklist for Manual Upgrade to Oracle Database 11gR2 (11.2) (Doc ID 837570.1)一.升 ...

  7. MySQL实用基本操作

    本博客写是装好Mysql并配好环境变量后的基本操作(windows10系统下)且都是黑框内的操作. 一.登陆MySQL 首先启动服务,在桌面左下角图标处点击右键Windows PowerShell(管 ...

  8. OpenGL——外部读档+异常报错

    从外部读取shader文件: 先添加Shader类: 再创建vertexSource.txt和fragmentSource.txt两个文件: 如图填入shader: 在shader.h宣告: 在sha ...

  9. Excel控制AutoCad进行坐标标注

    做过工程测绘,平面设计,使用过Autocad制图的朋友们,都经常要在CAD上标注点或者线的坐标,CAD自身的标注功能,并不能同时标注X和Y坐标,,要同时标注X和Y坐标,可以使用南方CASS软件,或者一 ...

  10. 发现一个新的注入 代码 eval

    下面这句代码,就是一段恶意的代码,通过form POST 提交数据即可生成脚本文件. eval('?>' . file_get_contents('php://input'));