LeetCode Linked List Cycle 解答程序
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
错误解法。因为Cycle可能出现在Link的中间的,所以需要检查其中间Nodes。
bool hasCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)
return false; ListNode *cur = head;
while(cur != NULL)
{
cur = cur->next;
if(cur == head)
return true;
}
return false;
}
正确解法:
class Solution {
public: bool find(ListNode *head, ListNode *testpNode)
{
ListNode *p = head;
while (p != testpNode->next)
{
if(p == testpNode && p != testpNode->next)
return false;
p = p->next;
}
return true;
} bool hasCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)
return false; ListNode *cur = head;
while(cur != NULL)
{
if(find(head, cur))
return true;
cur = cur->next;
}
return false;
}
};
LeetCode Linked List Cycle 解答程序的更多相关文章
- 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 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle 解题报告
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
随机推荐
- redux 初步理解
派发一个 action 给 reducer, reducer 生成了一个新的 state; redux 通过 Store 来保存数据, store.getState 获得数据, 而要更新 state, ...
- 【UOJ #204】【APIO 2016】Boat
http://uoj.ac/problem/204 肯定要离散化的,先离散化出\(O(n)\)个取值区间. 设\(f(i,j)\)表示第\(i\)所学校派出的划艇数量在\(j\)区间中. \(f(i, ...
- codevs 5294 挖地雷
5294 挖地雷 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个地图上有N个地窖(N<=20),每个地窖中埋有一 ...
- 【SPFA+二分答案】BZOJ1614- [Usaco2007 Jan]Telephone Lines架设电话线
沉迷于刷水 以前的那个二分写法过不了QAQ 换了一种好像大家都比较常用的二分.原因还不是很清楚. [题目大意] 给出一张图,可以将其中k条边的边权减为0,求1到n的路径中最长边的最小值. [思路] 二 ...
- python爬虫我是斗图之王
python爬虫我是斗图之王 本文会以斗图啦网站为例,爬取所有表情包. 阅读之前需要对线程池.连接池.正则表达式稍作了解. 分析网站 页面url分析 打开斗图啦网站,简单翻阅之后发现最新表情每页包含的 ...
- 最短路:我的理解--Dijkstra算法
最短路径:Dijkstra算法 用来计算从一个点到其他所有点的最短路径的算法,是一种单源最短路径算法.也就是说,只能计算起点只有一个的情况. Dijkstra的时间复杂度是O (N2),它不能处理存在 ...
- 在centos6.0上通过nginx远程执行shell
nginx本身不支持直接调用shell脚本,我们可以通过安装fastcgi程序,让nginx把调用shell的http请求交给fastcgi程序去处理,然后nginx 再将结果返回给用户方式间接调用s ...
- Cocos2d-x3.0 解压zip
2dx3.0为我们集成了unzip库,帮助我们实现对文件的解压,但使用起来略显复杂我这里封装了一个解压工具库.分享一下. 工具类下载:http://download.csdn.net/detail/q ...
- busdog is a filter driver for MS Windows (XP and above) to sniff USB traffic.
https://code.google.com/p/busdog/ busdog is a filter driver for MS Windows (XP and above) to sniff U ...
- SD卡 TF卡 接口引脚定义