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?
题目标签:Linked List
题目给了我们一个 Linked List,让我们判断它是否循环。
利用快,慢指针,快指针一次走2步,慢指针一次走1步,如果循环,快慢指针一定会相遇。
Java Solution:
Runtime beats 98.15%
完成日期:06/09/2017
关键词:singly-linked list;cycle
关键点:fast, slow pointers
1 /** 2 * Definition for singly-linked list. 3 * class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution 13 { 14 public boolean hasCycle(ListNode head) 15 { 16 if(head == null) 17 return false; 18 19 ListNode slow = head; 20 ListNode fast = head; 21 22 do 23 { 24 if(fast.next == null || fast.next.next == null) // if fast reaches to end, it doens't have a cycle 25 return false; 26 27 fast = fast.next.next; // fast moves 2 steps 28 slow = slow.next; // slow moves 1 step 29 30 31 } while(fast != slow); 32 33 return true; // if fast catches slow, meaning it has a cycle 34 } 35 }
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
转载于:https://www.cnblogs.com/jimmycheng/p/7883030.html
LeetCode 141. Linked List Cycle (链表循环)相关推荐
- [LeetCode] 141. Linked List Cycle 单链表判圆算法
TWO POINTER 快指针速度2 , 慢指针速度1 相对速度1,有环必然相遇 public class Solution {public boolean hasCycle(ListNode hea ...
- 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
用快慢指针来判定是否有环. 这里while loop里的条件,用的是fast.next != null && fast.next.next != null,保证如果没有环,slow一定 ...
- Leetcode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 141. Linked List Cycle
欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法
题目地址:Linked List Cycle - LeetCode Given a linked list, determine if it has a cycle in it. To represe ...
最新文章
- 专家认为自动驾驶汽车需要很多年的五个原因
- 小本创业的11个步骤
- VMware中装Win2012并配置Hyper-v
- 机器学习的Agile过程
- 单片机成长之路(51基础篇) - 022 N76e003 APROM模拟EEPROM驱动
- 跨域调用WebApi
- 约瑟夫问题的学习(基于循环链表)以及基于循环数组
- MAC IOS ssh 连接下修改环境变量
- mac删除android sd卡,如何从mac完全删除android及其所有文件?
- ctypes python_[python学习之路]ctypes,Python
- Pandas set_indexreset_index
- 查生日代码_让库克亲自送上生日祝福的10后小学生,还在B站教人学编程?
- [原]Jenkins(二十一) jenkins再出发Build periodically和Poll SCM
- p51 thinkpad 拆解_ThinkPad P51值得买吗?联想ThinkPad P51移动工作站图解评测
- bootstrap在线定制工具
- FRM 5.1 现代投资组合理论
- javascript写的日历控件(收藏)
- [生存志] 第145节 班固著汉书
- IDEA 黄色警告 found duplicated code in this file finds duplicated code
- 学习Bit Twiddling Hacks