Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
判断链表中是否有环,不能用额外的空间,可以使用快慢指针,慢指针一次走一步,快指针一次走两步,若是有环则快慢指针会相遇,若是fast->next==NULL则没有环。
值得注意的是:在链表的题中,快慢指针的使用频率还是很高,值得注意。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head)
{
ListNode *pFast=head;
ListNode *pSlow=head; while(pFast&&pFast->next)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
if(pFast==pSlow)
return true;
}
return false;
}
};
[Leetcode] Linked list cycle 判断链表是否有环的更多相关文章
-
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 lis ...
-
LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
-
[LeetCode] 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(判断链表是否有环)
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. Follow up:Can you solve it without using ext ...
-
141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
-
[CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
-
[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 ...
-
[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 ...
随机推荐
-
table 边框
<table width="100%" border="0" cellpadding="2" cellspacing="0& ...
-
Ubuntu gmake: command not found
由于ubuntu上取消了gmake(GUNmake)的而全部使用make代替.所以此问题的解决方式是有两种: 1.makefile中 gmake用make代替: 2.创建一个make的gmake连接: ...
-
Requirements Gathering
Requirements gathering is an essential part of any project and project management. Understanding ful ...
-
poj2774 Long Long Message(后缀数组)
[题目链接] http://poj.org/problem?id=2774 [题意] A & B的最长公共子序列. [思路] 拼接+height数组.将AB拼接成一个形如A$B的串,枚举hei ...
-
JSON 数字排序 多字段排序
原文http://bbs.csdn.net/topics/390594744?page=1#post-395599672 //排序数组 function SortBy(field, reverse, ...
-
clamwin + 拖拽查毒+右键查毒
下载 clamwin 到 windows 并安装 http://www.clamwin.com/ 为了方便使用clamwin,写一个bat,实现拖拽到bat 自动查毒 @echo off mode c ...
-
将代码从 spark 1.x 移植到 spark 2.x
1. SparkSession sparkSession可以视为sqlContext和hiveContext以及StreamingContext的结合体,这些Context的API都可以通过spark ...
-
Linux设备驱动剖析之IIC(三)
下面以eeprom用户程序调用ioctl函数的写操作为例追踪IIC子系统的调用过程.eeprom的用户测试是大部分开发板都自带的.看写一个字节数据的eeprom_write_byte函数的定义: in ...
-
cocos2d JS-(JavaScript) cc.each循环遍历对象
有了它,妈妈再也不用担心我的数组会越界啦!! each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSO ...
-
golang 垃圾回收机制
用任何带 GC 的语言最后都要直面 GC 问题.在以前学习 C# 的时候就*读了一大堆 .NET Garbage Collection 的文档.最近也学习了一番 golang 的垃圾回收机制,在这里 ...