[Algorithm] 11. Linked List Cycle
Description
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Example
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Challenge
Follow up:
Can you solve it without using extra space? (O(1) (i.e. constant) memory)?
Solution
/**
* 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) return false; ListNode *fast = head;
ListNode *slow = head; while(true){
// If a node is NULL, there is no cycle.
if(fast->next == NULL || fast->next->next == NULL) return false; slow = slow->next;
fast = fast->next->next; // When the fast and the slow run into the same node, there's a cycle.
if ( slow->val == fast->val )
return true;
}
}
};
[Algorithm] 11. Linked List Cycle的更多相关文章
- 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 ...
- [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 中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: 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, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [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 ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
随机推荐
- js执行“按回车”的动作
<textarea class="W_input" style="overflow: hidden; height: 23px;" node-type=& ...
- SuperSocket中的Server是如何初Initialize的
第一个函数 d:\sourcecode\github\supersocket\quickstart\basic\telnetserver_startbyconfig\program.cs static ...
- YTU 2625: B 构造函数和析构函数
2625: B 构造函数和析构函数 时间限制: 1 Sec 内存限制: 128 MB 提交: 772 解决: 513 题目描述 在建立类对象时系统自动该类的构造函数完成对象的初始化工作, 当类对象 ...
- mysql —— 利用Navicat 导出和导入数据库
Navicat for MySql 导出数据库方法: 打开Navicat for MySql,在要导出的数据库上面右击鼠标,点击“转储SQL 文件”→“数据和结构”. 找到合适的路径,点击“保存”. ...
- struct框架
配置文件struct-config.xml<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE str ...
- bzoj1528 sam-Toy Cars(贪心,优先队列)
「BZOJ1528」[POI2005] sam – Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Ja ...
- Winform执行CMD命令
1.首先分享CmdHelper类: using System; using System.Collections.Generic; using System.Text; using System.Di ...
- redis在linux的安装和开机启动(二)
编译 安装 makefile已经存在 执行make 即可 make之后, 自动创建可运行的脚本文件, 不需要再执行 install了. 将脚本文件, 拷贝到指定位置, 就可以了. 手动创建目录, 需要 ...
- Linux之线程相关命令及常用命令
查进程 top命令:查看系统的资源状况.#top top -d 10 //指定系统更新进程的时间为10秒 ps:查看当前用户的活动进程.#ps -A ps命令查找与进程相关的PID号: ps ...
- 组合数学题 Codeforces Round #108 (Div. 2) C. Pocket Book
题目传送门 /* 题意:每一次任选i,j行字符串进行任意长度前缀交换,然后不断重复这个过程,问在过程中,第一行字符串不同的个数 组合数学题:每一列不同的字母都有可能到第一行,所以每列的可能值相乘取模就 ...