leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:
判断一个链表是否有环。
解决方案:
双指针,快指针每次走两步,慢指针每次走一步,
如果有环,快指针和慢指针会在环内相遇,fast == slow,这时候返回true。
如果没有环,返回false.
/**
* 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 fast = head, slow = head;
if(head == null || head.next == null) return false; while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next; if(fast == slow){
return true;
}
} return false;
}
}
leetcode 141. Linked List Cycle的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [LeetCode] 141. Linked List Cycle 链表中的环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- LeetCode 141. Linked List Cycle (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- leetcode 141 Linked List Cycle Hash fast and slow pointer
Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...
- Java for LeetCode 141 Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- leetcode 141. Linked List Cycle ----- java
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- Python [Leetcode 141]Linked List Cycle
题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...
随机推荐
- BZOJ4446: [Scoi2015]小凸玩密室
用ui,j表示走完i的子树后走到i的深度为j的祖先的兄弟的最小代价: 用vi,j表示走完i的子树后走到i的深度为j的祖先的最小代价,用u算出v. 枚举起点,计算答案. #include<bits ...
- 【原】React操作表单
最近的项目中开发中都是用react,其中有用到react去操纵表单.然后自己就在每个表单元素中添加 ref, 然后再像jquery操作dom一样去操纵这个ref, 代码如下: 首先我在每个表单元素那 ...
- [转发]Dumps of system information with Apple computers
In this article, I gathered up all the dumps, who found. If you see something new table will be upda ...
- 分页探究--Filter+JSTL
最近卡了一个功能就是分页,查了很多资料,分页大概是两种类型:一种是把数据库的东西全部查出来然后放在session里,用list一页一页传到页面,这样的消耗比较大;另一种就是使用sql语句的limit来 ...
- mysql 字符串
mysql中一个字符串,既可以用两个单引号表示,也可以用两个双引号表示. 比如字符串 wangxiaowei,用单引号表示 'wangxiaowei',双引号表示"wangxiaowei&q ...
- bs4_2
QQ:231469242 欢迎交流 Parsing HTML with the BeautifulSoup Module Beautiful Soup是用于提取HTML网页信息的模板,Beautif ...
- Starling Tutorial
http://www.hsharma.com/tutorials/starting-with-starling-ep-1-intro-setup/
- Construct Bounding Sphere
点集的包围球 http://en.wikipedia.org/wiki/Bounding_sphere http://blogs.agi.com/insight3d/index.php/2008/02 ...
- webpack入坑之旅(一)入门安装
学习一个新的东西,首先第一步就是安装,有时候会遇到各种奇葩的问题 至于什么是webpack我这里就不介绍了,请参考官网:https://github.com/webpack/webpack 安装 前提 ...
- Ubuntu用作Server时出现乱码的解决方法
下面给出解决办法: 1.用vi编辑器修改/etc/default/local文件 2.把原来的中文编码替换成下面的 LANG="en_US.UTF-8" LANGUAGE=&quo ...