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:

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的更多相关文章

  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. Linux下安装Python3的django并配置mysql作为django默认数据库(转载)

    我的操作系统为centos6.5 1  首先选择django要使用什么数据库.django1.10默认数据库为sqlite3,本人想使用mysql数据库,但为了测试方便顺便要安装一下sqlite开发包 ...

  2. php闭包的使用实例

    $childrenNodes = array_filter($list, function($item) use($parentId){ return $item->node_pid == $p ...

  3. c++ 创建线程以及参数传递

    //创建线程,传递参数 DWORD dwThreadID = ; HANDLE hThread = CreateThread(NULL, , MonitorThreadFunction, , & ...

  4. 台式机安装Linux操作系统无法识别网卡

    在公司一台台式机上安装centos7系统,发现安装好之后,发现没有自动生成eth0网卡. 查看网卡相关信息: lspci|grep Eth 获取到网卡型号后,可以去官网下载对应的安装包进行编译安装即可 ...

  5. edgedb 内部pg 数据存储的探索 (一)基本环境搭建

    edgedb 是基于pg 上的对象关系数据库,已经写过使用docker 运行的demo,为了探索内部的原理,做了一下尝试,开启pg 访问 后边会进一步的学习 环境准备 为了测试,使用yum 安装 安装 ...

  6. 宝塔linux面板 解决TP3.2 404

    在配置文件中加入一下配置: location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1; } } location ...

  7. Kafka入门 --安装和简单实用

    一.安装Zookeeper 参考: Zookeeper的下载.安装和启动 Zookeeper 集群搭建--单机伪分布式集群 二.下载Kafka 进入http://kafka.apache.org/do ...

  8. VS2012统计代码量

    第一步:打开项目 第二步:CTRL+SHIFT+H  选择在文件中查找,输入 表达式:b*[^:b#/]+.*$,如下图所示: 第三步:单击 查找全部 按钮,统计结果如下图所示:

  9. 使用 GDB 调试需要命令行参数的程序

    使用 gdb 命令提供的 --args 选项可以调试需要命令行参数的程序,如下: gdb --args a.out arg1 arg2 arg3

  10. 华为PAY公交卡建议开卡免费!

    华为PAY公交卡,大家都知道是华为与当地交通卡通公司合作的,开卡费大概15-29元,最低充值10-30元. 估计大部分开卡费是给了当地交通卡公司,华为也应该有收入分成.这个虚拟公交卡,不同于实体公交卡 ...