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 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的更多相关文章
- 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 ...
随机推荐
- asp.net实现伪静态
一.配置应用程序 1.下载URLRewrite.dll,程序中添加引用 2.在web.config中配置 <configuration> <configSections> &l ...
- a链接中 JS弹出确认对话框方法
一种: <a href="javascript:if(confirm('确实要删除该内容吗?'))location='http://www.google.com'">弹 ...
- as 报错
报错: cantnot find the declaration of element 'LinearLayout' 解决: 原本为了解决报错我把Android support关掉了,然后百度到解决办 ...
- Python练习七
1.写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者. def func(dic): for k in dic: if len(dic[k] ...
- docker WARNING: IPv4 forwarding is disabled. 解决方法
WARNING: IPv4 forwarding is disabled. Networking will not work. 在宿主机添加如下信息 echo net.ipv4.ip_forward= ...
- Unity3D外包 团队更新一下UE4和Unity3D案例
欢迎联系我们索取,谢谢! 有项目外包请联系QQ:372900288 索取案例.
- 使用Let's Encrypt搭建永久免费的HTTPS服务
1.概述1.1 HTTPS概述HTTPS即HTTP + TLS,TLS 是传输层加密协议,它的前身是 SSL 协议.我们知道HTTP协议是基于TCP的.简而言之HTTPS就是在TCP的基础上套一层TL ...
- python-pcl
python-pcl安装和使用 https://blog.csdn.net/joker_hapy/article/details/85006818 Ubuntu16.04下安装PCL及python-p ...
- function "round" declared implicitly
keil工程代码,浮点计算中引用了数学库 math.h 中的round函数,但编译时出现告警 “warning: #223-D: function "round" declare ...
- 升级GCC以支持C++11
本文主要介绍在Linux系统下,如何升级GCC以支持C++11.目前来看GCC是对C++11支持程度最高最多的编译器,但需要GCC4.8及以上版本. 本文使用操作系统:Centos 6.4 Desk ...

