[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 ...
随机推荐
- YTU 2631: B1 能存各种类型数据的Store类
2631: B1 能存各种类型数据的Store类 时间限制: 1 Sec 内存限制: 128 MB 提交: 245 解决: 177 题目描述 有一种类,海纳百川,可以对任意类型的数据进行存取,造就 ...
- Java 技术体系(JDK 与 JRE 的关系)、POJO 与 JavaBeans
Java 技术体系的分层结构(不同的颜色表示不同的层次),尤其注意 JDK 与 JRE 之间的包含关系: 图见 Java Platform Standard Edition 7 Documentati ...
- flask装饰器route实现路由功能理解
利用装饰器的方式实现了路由函数,这是一个十分简单清晰的结构,而这个功能的实现,有着很大的学习意义 @appweb.route('index',methods=['GET','POST'] def st ...
- emma中文显示乱码问题解决(ubutnu)
vim -/.emma/emmarc 找到 db_encoding=latin1 改为 db_encoding=utf8 然后重新运行emma,此时发现还是乱码,不要着急,在执行所有的sql语句 ...
- vue 加载文件,省略后缀后的加载顺序
Vue使用import ... from ...来导入组件,库,变量等.而from后的来源可以是js,vue,json.这个是在webpack.base.conf.js中设置的: module.exp ...
- sql server数据库占用cpu太大,使用sys.dm_exec_query_stats查询优化
查询sql语句占用 CPU详细信息: SELECT (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , ( (CASE WHE ...
- bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】
仿佛学到了贪心的新姿势-- 考虑相邻两头牛,交换它们对其他牛不产生影响,所以如果交换这两头牛能使这两头牛之间的最大值变小,则交换 #include<iostream> #include&l ...
- [App Store Connect帮助]六、测试 Beta 版本(4.4) 管理 Beta 版构建版本:停止测试构建版本
在首页上,点按“我的 App”,选择您的 App,然后在工具栏中点按“TestFlight”. 在左列中的“构建版本”下,点按您 App 的平台(iOS 或 Apple TVOS). 在右表中,点按该 ...
- easyui-datebox 年月视图显示
//年月视图做法 $('#startYearDate').datebox({ onShowPanel: function () { //显示日趋选择对象后再触发弹出月份层的事件,初始化时没有生成月份层 ...
- jQuery——表单应用(4)
HTML: <!--复选框应用--> <!DOCTYPE html> <html> <head> <meta charset="UTF- ...