LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list. 判断链表是否有环:使用快慢指针去判断,如果有环,fast快指针先进入环,slow慢指针后进入,经过一定步的操作,快慢指针在环上相遇。方法一:(C++)
1 class Solution { 2 public: 3 bool hasCycle(ListNode *head) { 4 ListNode *fast=head,*slow=head; 5 while(fast&&fast->next){ 6 slow=slow->next; 7 fast=fast->next->next; 8 if(slow==fast) 9 return true; 10 } 11 return false; 12 } 13 };
方法二:(Java)
1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 Set<ListNode> s=new HashSet(); 4 while(head!=null){ 5 if(s.contains(head)) 6 return true; 7 else 8 s.add(head); 9 head=head.next; 10 } 11 return false; 12 } 13 }
Java集合中含有contains()方法,判断其是否已经含有此节点,如果没有,则使用add()添加进去
转载于:https://www.cnblogs.com/hhhhan1025/p/10582829.html
LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java相关推荐
- 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 单链表判圆算法
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 ...
- 141. Linked List Cycle 环形链表
给定一个链表,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引 ...
- 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. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- Leetcode 141. Linked List CycleJAVA语言
1 2 3 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without us ...
- 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:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
最新文章
- exchange 2010备份及恢复
- 病毒入侵计算机的危害,那些年电脑中过的病毒,中病毒的危害又有哪些
- 3 saltstack高可用
- kafka自带没web ui界面,怎么办?安装个第三方的
- 洛谷 - P2761 软件补丁问题(spfa+状压)
- C++中的inline用法
- matlab 文件指针回到开头,[c/c++] 文件指针位置回到文件开头(rewind)及行开头(ftell+fseek)...
- C++/C--浮点型数据的二进制表示及其内存存储形式
- mt4 不显示服务器速度,mt4上面怎么显示时间?
- 驱动拦截NT的API实现隐藏木马客户端
- android的log.v,Android Log.v(),Log.d(),Log.i(),Log.w(),Log.e() - 何时使用每一个?
- FastAPI集成SQLAlchemy实现数据库操作
- 【UNIX环境高级编程】UNIX基础知识
- Oracle卸载方法
- C++获取、设置鼠标坐标并移动鼠标
- unet医学肺部ct图分割简单记录
- 免费电子书 工具 开发工具包 各类资源 下载
- CDR智能填充—图形颜色填充好帮手
- 恢复通讯录显示服务器开小差,恢复备份数据通讯录还是没有找到数据怎么办?...
- 计算机的系统保护选项,右击“我的电脑”,属性选项,没有“系统还原”怎么处理啊?...