leetcode141: Linked List Cycle
来源:互联网 发布:李婧磁 知乎 编辑:程序博客网 时间:2024/06/03 10:52
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?//不能创建额外的空间
题目要求是判断链表是否有环,leetcode上链表的题都是没有头结点的,这点大家要记住。而且若链表有环,也是最后一个节点形成的环。
大家考虑这样一个问题,链表的环相当于一个圆形操场。假设有两个人在圆形操场上无限循环的跑,那么速度快的一定能追得上速度慢的。同理,若要判断一个链表是否有环,可设计快慢指针,当快慢指针都进入环的时候,若最终两个指针相遇,必可说明链表存在环。下面就要考虑快慢指针的步长,从跑操场的情况来看,不管慢的有多慢,快得有多快,最终肯定能相遇。同理,链表中,也可随意指定快慢指针的步长,无非就是跑的圈数多少的问题。
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) { ListNode p=head;//快指针 ListNode q=head;//慢指针 while(p!=null&&q!=null&&p.next!=null){//边界条件是出现空指针,就返回false; q=q.next; p=p.next.next;//空指针没有next,否则会出现NullPointerException问题 if(p==q)return true; } return false; }}
0 0
- LeetCode141:Linked List Cycle
- leetcode141. Linked List Cycle
- leetcode141: Linked List Cycle
- leetcode141 Linked List Cycle
- LeetCode141. Linked List Cycle
- leetcode141. Linked List Cycle
- leetcode141~Linked List Cycle
- LeetCode141. Linked List Cycle
- leetcode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- leetcode141. Linked List Cycle
- LeetCode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- LeetCode141—Linked List Cycle
- LeetCode141之Linked List Cycle java题解
- Leetcode141 - Linked List Cycle(链表)
- javascript学习——创建对象
- Tomcat启动报错 Failed to initialize connector [Connector[HTTP/1.1-8080]]
- autoreleasepool底层详解
- c#泛型使用详解:泛型特点、泛型继承、泛型接口、泛型委托
- Dubbo分布式服务框架入门(附工程)
- leetcode141: Linked List Cycle
- TFS文件系统 安装
- ImageView设置成手机内存卡图片
- C++ num++书写位置影响其值的输出问题
- Non-standard evaluation
- Java Bean 还是 POJO, 究竟用哪个好?
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- JAVA多线程和并发基础面试问答
- 【BZOJ 1614】: [Usaco2007 Jan]Telephone Lines架设电话线 spfa+二分