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. this在java中的用法

    this在java中的用法 1.使用this关键字引用成员变量 作用:解决成员变量与参数或局部变量命名冲突的问题 public class Dog { String name; public Dog( ...

  2. java8新特性学习笔记链接

    https://blog.csdn.net/yitian_66/article/details/81010434

  3. 2019-04-15 Python中的面向对象学习总结

    一.面向对象总结: (1)三要素:封装,继承,多态                                       详细介绍链接:https://www.jianshu.com/p/68a ...

  4. python 不知道是啥

    1.判断两个大文件是否是同一个文件 import os import hashlib import time start = time.time() path1 = r"E:\视频资料\el ...

  5. python基础之 time,datetime,collections

    1.time模块 python中的time和datetime模块是时间方面的模块 time模块中时间表现的格式主要有三种: 1.timestamp:时间戳,时间戳表示的是从1970年1月1日00:00 ...

  6. 创建存储过程 in,out,inout

    in: 输入参数,存储过程如果修改了参数值,那么不能被返回. out:输出参数,存储过程中修改了参数值,可以被返回.inout:输入参数,存储过程如果修改了参数值,可以被返回 注意参数格式:  in  ...

  7. openpyxl.utils.exceptions.IllegalCharacterError

    如果是手动调用 xlwt 这种第三方库除了错可能没法找错误,但是从错误中我们看到错误是由 openpyxl 抛出的,我们试着从 openpyxl 中找解决方案 出错处的代码 value = value ...

  8. PHP 获取一篇文章内容中的全部图片,并下载

    做个记录,在工作or面试中有可能会遇到function downImagesFromTargetUrl($url, $target_dir = null) { if(!filter_var($url, ...

  9. parcel 在js中导入 html 文件

    parcel不支持将html文件导入为字符串,如果您对parcel使用熟练,直接使用 parcel-plugin-phtml 插件即可,此插件使用 .phtml 后缀 为什么用parcel? 因为从我 ...

  10. DataGridView 列排序 内存表查找

    DataRow[] drow = dt.Select("列名 = 列名的值" ); 就这句话,dt是一个datatable 且断点调试时能看到里面有trade这个列,可为什么执行到 ...