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 poswhich 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:

  1. Input: head = [3,2,0,-4], pos = 1
  2. Output: true
  3. Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

  1. Input: head = [1,2], pos = 0
  2. Output: true
  3. Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

  1. Input: head = [1], pos = -1
  2. Output: false
  3. 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. };
  1. 方法二:(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()添加进去

  1.  
  1.  
  1.  
  1.  

LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java的更多相关文章

  1. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. cordova文件系统插件的使用方法:cordova-plugin-file

    提供对设备上的文件进行读取和写入的功能支持. 1. 添加插件:cordova plugin add cordova-plugin-file 2. 调用方法:

  2. Dubbo的三种连接方式

    1.采用zookeeper作为注册中心 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  3. Java(全局变量-静态变量-位运算符)

    全局变量是默认赋值的:而局部变量是没有默认赋值的(需要赋值才能使用)静态变量只能被静态方法使用 位运算右移,相当于做除法,2的n次幂00001000操作的位移数相当于是偏移量从右向左数n位,从第n+1 ...

  4. scipy 的K-means

    #导入相应的包 import scipy import scipy.cluster.hierarchy as sch from scipy.cluster.vq import vq,kmeans,wh ...

  5. c# 获取 com 引用真实组件地址

    1.根据guid获取 var clsid = new Guid("63EA2B90-C5A8-46F4-8A6E-2F2436C80003").ToString("B&q ...

  6. 深度系统 deepin 15.9 关闭桌面

    深度系统 deepin 15.9 关闭桌面 由于特别的原因,关闭深度的桌面. sudo systemctl disable lightdm 如果需要在命令模式进入桌面可以使用以下命令. sudo se ...

  7. Zabbix 3.4 服务端安装部署

    关于zabbix的安装部署官方也提供了详细的安装文档,链接如下: https://www.zabbix.com/download 选择zabbix的版本,服务器平台及使用的数据库 安装和配置zabbi ...

  8. PHP-不同Str 拼接方法性能对比 参考自https://www.cnblogs.com/xiaoerli520/p/9624309.html

    问题 在PHP中,有多种字符串拼接的方式可供选择,共有: 1 . , .= , sprintf, vprintf, join, implode 那么,那种才是最快的,或者那种才是最适合业务使用的,需要 ...

  9. Open Source 开发工具集

    Open Source 开发工具集 转自:http://www.linuxforum.net原作者:gogoliu(Pooh-Bah) 编辑器: vi:老牌编辑器,在各个unix和unix-like平 ...

  10. 视频流PS,PS封装H264

    出处: ISOIEC 13818-1 PS流: PS流由PSGOP组成,每个PSGOP是由I帧起始的多帧集合,每个GOP之间没有相互依赖信息,可以剪切拼接. | PSGOP0 | PSGOP1 | P ...