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 ...
随机推荐
- NumPy简明教程(二、数组1)
NumPy数组 NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成: 实际的数据 描述这些数据的元数据 大部分操作仅针对于元数据,而不改变底层实际的数据. 关于NumPy数组有几点必 ...
- JMS介绍:我对JMS的理解和认识
[ZT]JMS介绍:我对JMS的理解和认识 转自:http://blog.csdn.net/KimmKing/archive/2011/06/30/6577021.aspx,感谢作者KimmKing ...
- 【BZOJ1098】[POI2007]办公楼biu
题目一开始看以为和强联通分量有关,后来发现是无向边,其实就是求原图的补图的联通块个数和大小.学习了黄学长的代码,利用链表来优化,其实就是枚举每一个人,然后把和他不相连的人都删去放进同一个联通块里,利用 ...
- 范浩强treap 普通平衡树
增加Split(分裂),Merge(合并)操作,非常好写,时间也不比普通treap慢什么. #include<bits/stdc++.h> using namespace std; str ...
- BZOJ3238 [Ahoi2013]差异 SA+单调栈
题面 戳这里 题解 考虑把要求的那个东西拆开算,前面一个东西像想怎么算怎么算,后面那个东西在建出\(height\)数组后相当于是求所有区间\(min\)的和*2,单调栈维护一波即可. #includ ...
- Java Queue的测试
上传图片没上去,提交的时候已经结束 代码链接
- 1.8(SQL学习笔记)触发器
一.触发器简介 当需要某些操作在某些语句执行之前或之后执行就需要使用触发器. 例如每次插入数据时进行数据校对,每次删除数据后将删除内容备份到新表. 这些操作我们希望它(某些语句)在满足某些条件时自动执 ...
- 【9.23校内测试】【抽屉原理】【乱搞??(找众数】【Trie】
看到题目一开始想到的是一道求子集和的异或和,可以用$bitset$实现求子集和.然而这道题如果要强算子集和肯定是带不动的,况且还要算方案,所以尝试去找题目中的性质. 看到整除,很容易想到如果是一段区间 ...
- ACM -- 算法小结(十)素数的两种打表法
素数的两种打表法 下面介绍两种素数打表法,由于是两年前留下的笔记,所以没有原创链接~~ @_@!! 第一种疯狂打表法: #include<stdio.h> #include<math ...
- April Fools Day Contest 2016 F. Ace It!
F. Ace It! 题目连接: http://www.codeforces.com/contest/656/problem/F Description Input The only line of ...