141. Linked List Cycle【easy】
141. Linked List Cycle【easy】
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
解法一:
/**
* 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 == NULL || head->next == NULL) {
return false;
} ListNode * slow = head;
ListNode * fast = head; while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next; if (fast == slow) {
return true;
}
} return false;
}
};
经典的快慢指针思想
141. Linked List Cycle【easy】的更多相关文章
- 141. Linked List Cycle【Easy】【判断链表是否存在环】
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- 102. Linked List Cycle【medium】
Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, tail ...
- LeetCode--LinkedList--141.Linked List Cycle(Easy)
141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
随机推荐
- [Codeforces 28D] Do not fear,DravDe is kind
Brief Intro: 对于四元组(v,c,l,r),求其子序列中v最大的和,并使其满足: 1.Ci+Li+Ri相同 2.L1=0,Rn=0 3.Li=Sigma(C1...Ci-1) Soluti ...
- 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones
打表找规律即可. 1,1,2,2,2,3,3,3,3,4,4,4,4,4... 注意打表的时候,sg值不只与剩下的石子数有关,也和之前取走的方案有关. //#include<cstdio> ...
- 【博弈论】【SG函数】【枚举】bzoj1874 [BeiJing2009 WinterCamp]取石子游戏
枚举第一步可能达到的状态,判断是否是必败态即可. #include<cstdio> #include<set> #include<cstring> using na ...
- nodejs全局变量设置设置
编辑 ~/.npmrc 加入下面内容 prefix = D:\tool\nodejs\node_global cache = D:\tool\nodejs\node_cache registry = ...
- iOS数据库操作(使用FMDB)
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
- log4j在Web项目中的使用
导入log4j的jar包 在web.xml上配置如下: <!-- 配置log4j begin --> <context-param> <param-nam ...
- 使用ReadOnlyCollection创建只读集合
转载:http://www.cnblogs.com/abatei/archive/2008/02/04/1064102.html 使用泛型创建只读集合 问题 您希望类中的一个集合里的信息可以被外界访问 ...
- VS2017安装错误:工作负荷不完整,未能安装包“sqlcmdlnutils,version=15.1.61703.130,chip=x64,language=zh-CN”。
场景:已安装的VS2017维护安装MVC4时出现如下错误: 看问题描述是由于sqlcmdlnutils安装失败影响到其它组件的安装,于是单独下载此安装包进行安装,发现安装一切正常,继续维护VS2017 ...
- Android ProgressBar手动控制开始和停止
这两天有个需求,点击按钮从SD卡解压压缩包,并读取压缩包内txt文档内容,然后在街面上显示出来.毕竟IO操作很耗时,如果文件较大会花费不少时间.所以,在处理数据的时候能给个进度就好了.我们通常的做法就 ...
- UNDO表空间损坏导致数据库无法OPEN
在数据库undo表空间文件损坏.或者undo表空间文件缺失的情况下.无法打开数据库. 这两种情况都能够视为一种情况处理,解决方法一样. 场景:在23:10的时候新建一个undo表空间undotbs02 ...