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. WAN口和LAN 口有什么区别

    WAN口不能够用来连接电脑. LAN(1.2.3.4)口只能够用来连接电脑. 拓展资料 路由器(Router),是连接因特网中各局域网.广域网的设备,它会根据信道的情况自动选择和设定路由,以最佳路径, ...

  2. python基础语法-->多项分支-->巢状分支

    # ### 多项分支 """ if 条件表达式: codel1... codel1... else 条件表达式 coedl2.. coedl2.. else 条件表达式 ...

  3. PyAutoGUI——让所有GUI都自动化

    2015-08-17:输入中文bug没有解决,目前的解决方案是Python 2.X环境下安装pyperclip和pyautogui,用复制粘贴来实现. In [ ]: import pyperclip ...

  4. git使用手册整理

    -------------------20181217------------------- git使用:在gitbash 下初始化用户: $ git config --global user.nam ...

  5. Emmet:HTML/CSS代码快速编写神器教程

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  6. dede织梦手机站m文件夹功能基础详解

    织梦2015年6月8日更新后,就添加了很多针对手机移动端的设计,最大的设计就是添加了生成二维码的织梦标签和织梦手机模板功能,织梦更新后,默认的 default模板中就包含手机模板,所以我们可以给织梦网 ...

  7. ubuntu装好jupyter启动失败问题

    ::/jupyter/nbserver-.json: [Errno ] Permission denied: '/run/user/1000/jupyter/nbserver-35390.json' ...

  8. CentOS 7 部署Gitlab+Jenkins持续集成(CI)环境

    持续集成概述及运行流程 : 持续集成概述 :持续集成(Continuous integration)持续集成是指开发者在代码的开发过程中 ,可以频繁的将代码部署集成到主干,并进行自动化测试  开发→代 ...

  9. 使用 dom4j 处理 xml (2)

    记录一些 xpath 的常规用法,备忘. //3.XPath 了解(用于在xml中选取元素,类似 jquery 选择器) //3.1 路径 Node node1 = root.selectSingle ...

  10. IO流(三)

    五.Java序列化 概述 Java序列化是指把Java对象转换为字节序列的过程 Java反序列化是指把字节序列恢复为Java对象的过程 当两个Java进程进行通信时,发送方需要把这个Java对象转换为 ...