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. C#进度条简单应用

    进度条表示文件复制的进度: 1.将进度条最大值设置为需要复制的文件总数 2.遍历文件时每复制一个文件之后,进度条+1 ;//文件总数 progressBar1.Value = progressBar1 ...

  2. SharePoint 已在此服务器场中安装 ID 为 XXXXXXXXX 的功能。请使用强制属性显式地重新安装此功能。解决方法

    图1: 图2: 解决方法: stsadm -o deploysolution -name ***.wsp -immediate -allowGacDeployment -url http://*** ...

  3. 如何修改运行中的docker容器的端口映射和挂载目录

    在docker run创建并运行容器的时候,可以通过-p指定端口映射规则.但是,我们经常会遇到刚开始忘记设置端口映射或者设置错了需要修改.当docker start运行容器后并没有提供一个-p选项或设 ...

  4. linux c tcp p2p

    江湖上一直都有这位哥哥的传说,也有很多人说自己就他的真身! 但是... 今天分享一下TCP连接的P2P demo,江湖的规矩也要与时俱进... ———————————————————————————— ...

  5. php爬虫入门

    本篇文章介绍PHP抓取网页内容技术,利用PHP cURL扩展获取网页内容,还可以抓取网页头部,设置cookie,处理302跳转. 一.cURL安装 采用源码安装PHP时,需要在configure时添加 ...

  6. .htaccess使用方法介绍

    1..htaccess文件使用前提 .htaccess的主要作用就是实现url改写,也就是当浏览器通过url访问到服务器某个文件夹时,作为主人,我们可以来接待这个url,具体地怎样接待它,就是此文件的 ...

  7. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  8. apache----------在apache环境下安装https支持

    1.安装mod_ssl  yum install mod_ssl2.修改阿帕奇的配置文件开启3.防火墙要开启443端口4.要把三个证书上传到阿帕奇配置文件下.5.更新 httpd.conf 配置文件 ...

  9. Java文件类型工具类

    package *; import java.util.HashMap; import java.util.Map; /** * <p> * <b>FileTypeEnum2& ...

  10. git之push

    git push :将本地的哪个分支推送到哪个远程主机上的哪个分支.因此明确主机.本地分支名.远程分支名这三个要素. git push命令用于将本地分支的更新,推送到远程主机.它的格式与git pul ...