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. ...
随机推荐
- 两个ArrayList之间求交并补
class ArraylistCalculate{ // 两个整数集求差集 public ArrayList<Integer> integerArrayListDifference( Ar ...
- Servlet学习1
1.首先在Tomcat的webapp目录下新建文件夹myWebapp,作为自己的web应用. 2.myWebapp下新建WEB-INF(必须这个名)目录,WEB-INF下新建classes目录放置se ...
- 关闭 synactive guixt. 在sap gui的右上角一个标志里,将 active guixt 选项去掉即可。
关闭 synactive guixt. 在sap gui的右上角一个标志里,将 active guixt 选项去掉即可.
- SSMP一次请求数据处理过程分析
控制器代码 @RequestMapping("/changeUserPwd") public TranMessage changeUserPwd(String oriPwd, St ...
- 两种库解析、构造 JSON
1.用CJSON库 1.1解析Json 需要解析的JSON文件: { "name":"Tsybius", , "sex_is_male":t ...
- windows下C++连接mysql
平台:windows c/c++ 编译器:vs2017 项目设置:项目属性页: 1.C/C++ ->常规->附加包含目录->C:\Program Files (x86)\MySQL\ ...
- react使用create-react-app创建的项目部署
一.在所有的项目代码编写完成后,react项目直接部署是无法正常访问的 1.问题一 网页无法正常的浏览器刷新,刷新会报404错,路由找不到页面 2.问题二 路由跳转后,浏览器后退按钮点击后,页面不刷新 ...
- 面试 -- 关于Activity的相关知识
本篇文章就是记录关于简单的Activity中的问题 问题一:当Activity中存在两个注册的Activity都是主界面的话,应用程序会报错吗?如果不报错,会把那个Activity当做主界面执行 &l ...
- 如何正确对用户密码进行加密?转自https://blog.csdn.net/zhouyan8603/article/details/80473083
本文介绍了对密码哈希加密的基础知识,以及什么是正确的加密方式.还介绍了常见的密码破解方法,给出了如何避免密码被破解的思路.相信读者阅读本文后,就会对密码的加密有一个正确的认识,并对密码正确进行加密措施 ...
- python练习--利用while循环和if语句,完成猜骰子的数字大小
#exampleimport random# 骰子投掷的随机叔numnum = random.randint(1,6)# 输入一个猜测的数字temp = input("请输入一个整数:&qu ...