LeetCode Linked List Cycle 单链表环
题意:给一个单链表,判断其是否出现环!
思路:搞两个指针,每次,一个走两步,另一个走一步。若有环,他们会相遇,若无环,走两步的指针必定会先遇到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) {
if(!head) return false;
ListNode * one=head, *two=head->next;
while(two && two->next && one!=two)
{
one=one->next;
two=two->next->next;
}
if(two&&two->next) return true;
else return false;
}
};
AC代码
LeetCode Linked List Cycle 单链表环的更多相关文章
- [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 ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
- [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 ...
- 【Algorithm | 链表】单链表“环”、“环的起点”、“环的长度”问题
参考资料 • Floyd判圈算法 { 链接 } • 单链表“环”.“环的起点”.环的长度”问题 { 链接 } 链表环的问题 一.判断链表有换 使用两个指针slow和fast.两个指针开始时均在头节点处 ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
随机推荐
- 如何在Eclipse中配置Tomcat服务器
之前使用MyEclipse来开发Web应用,可以在MyEclipse中配置服务器,配置完后,直接运行服务器即可,很方便. 最近切换到Eclipse开发环境,发现使用Tomcat的方式不太一样,因此在此 ...
- spring配置事务
一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...
- css的transition 属性
把鼠标指针放到 div 元素上,其宽度会从 100px 逐渐变为 300px: div { width:100px; transition: width 2s; -moz-transition: wi ...
- 【BZOJ】【2132】圈地计划
网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...
- [设计模式] 8 组合模式 Composite
DP书上给出的定义:将对象组合成树形结构以表示“部分-整体”的层次结构.组合使得用户对单个对象和组合对象的使用具有一致性.注意两个字“树形”.这种树形结构在现实生活中随处可见,比如一个集团公司,它有一 ...
- Ignore files which are already versioned
If you accidentally added some files which should have been ignored, how do you get them out of vers ...
- SilverLight页面跳转(转载)
// Silverlight页面的跳转 // (Application.Current.RootVisualasIContent).Content=newDragControl(); //Silver ...
- AIZU 0005
GCD and LCM Time Limit : 1 sec, Memory Limit : 65536 KB Japanese version is here GCD and LCM Write a ...
- POJ 1775
#include <iostream> using namespace std; ,,,,,,,,,}; bool boo; void DFS(int time,int sum); int ...
- 李洪强漫谈iOS开发[C语言-038]-if else if语句
李洪强漫谈iOS开发[C语言-038]-if else if语句