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++)
- class Solution {
- public:
- bool hasCycle(ListNode *head) {
- ListNode *fast=head,*slow=head;
- while(fast&&fast->next){
- slow=slow->next;
- fast=fast->next->next;
- if(slow==fast)
- return true;
- }
- return false;
- }
- };
- 方法二:(Java)
- public class Solution {
- public boolean hasCycle(ListNode head) {
- Set<ListNode> s=new HashSet();
- while(head!=null){
- if(s.contains(head))
- return true;
- else
- s.add(head);
- head=head.next;
- }
- return false;
- }
- }
Java集合中含有contains()方法,判断其是否已经含有此节点,如果没有,则使用add()添加进去
LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java的更多相关文章
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [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 ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- [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 ...
- 141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
- [Leetcode] 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. Follow up:Can you solve it without using ext ...
随机推荐
- cordova文件系统插件的使用方法:cordova-plugin-file
提供对设备上的文件进行读取和写入的功能支持. 1. 添加插件:cordova plugin add cordova-plugin-file 2. 调用方法:
- Dubbo的三种连接方式
1.采用zookeeper作为注册中心 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...
- Java(全局变量-静态变量-位运算符)
全局变量是默认赋值的:而局部变量是没有默认赋值的(需要赋值才能使用)静态变量只能被静态方法使用 位运算右移,相当于做除法,2的n次幂00001000操作的位移数相当于是偏移量从右向左数n位,从第n+1 ...
- scipy 的K-means
#导入相应的包 import scipy import scipy.cluster.hierarchy as sch from scipy.cluster.vq import vq,kmeans,wh ...
- c# 获取 com 引用真实组件地址
1.根据guid获取 var clsid = new Guid("63EA2B90-C5A8-46F4-8A6E-2F2436C80003").ToString("B&q ...
- 深度系统 deepin 15.9 关闭桌面
深度系统 deepin 15.9 关闭桌面 由于特别的原因,关闭深度的桌面. sudo systemctl disable lightdm 如果需要在命令模式进入桌面可以使用以下命令. sudo se ...
- Zabbix 3.4 服务端安装部署
关于zabbix的安装部署官方也提供了详细的安装文档,链接如下: https://www.zabbix.com/download 选择zabbix的版本,服务器平台及使用的数据库 安装和配置zabbi ...
- PHP-不同Str 拼接方法性能对比 参考自https://www.cnblogs.com/xiaoerli520/p/9624309.html
问题 在PHP中,有多种字符串拼接的方式可供选择,共有: 1 . , .= , sprintf, vprintf, join, implode 那么,那种才是最快的,或者那种才是最适合业务使用的,需要 ...
- Open Source 开发工具集
Open Source 开发工具集 转自:http://www.linuxforum.net原作者:gogoliu(Pooh-Bah) 编辑器: vi:老牌编辑器,在各个unix和unix-like平 ...
- 视频流PS,PS封装H264
出处: ISOIEC 13818-1 PS流: PS流由PSGOP组成,每个PSGOP是由I帧起始的多帧集合,每个GOP之间没有相互依赖信息,可以剪切拼接. | PSGOP0 | PSGOP1 | P ...