Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
绕环n-1圈加上c的距离。

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL){
return 0;
}
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
break;
}
}
if(fast == NULL || fast->next == NULL){
return NULL;
}
slow = head;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can的更多相关文章
- [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- [Linked List]Remove Nth Node From End of List
Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...
- [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 & 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 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 142. Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- Leetcode Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 15. 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 solve i ...
随机推荐
- 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________。A.客观地验证需求管理活动
需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________.A.客观地验证需求管理活动 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是_________ ...
- 【algorithm】二叉树的遍历
二叉树的遍历 二叉树用例 代码解析: public class BinaryTree { static class TreeNode { Integer val; TreeNode left; Tre ...
- python经典一百道习题(转自奶酪博客)
无论学习哪门计算机语言,只要把100例中绝大部分题目都做一遍,就基本掌握该语言的语法了. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filena ...
- sql 使用汇总(PQSQL)
--sql structured query language --DML--Data Manipulation Language--数据操作语言 query information (SELECT) ...
- Win10系统64位快速专业安装版 V2016年
win10系统64位快速专业安装版 V2016年2月 系统下载:http://www.xitongma.com/ Ghost Win10 64位正式装机专业版2016 微软向Windows用户推送了w ...
- COGS 788. 昵称
788. 昵称 ★☆ 输入文件:nickname.in 输出文件:nickname.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] ZSUQ送信者与腾讯QQ相似 ...
- strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用
strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切 @property (nonatomic, strong) NSString *stringA; @property ...
- HDOJ 4509 湫湫系列故事——减肥记II(2013腾讯编程马拉松) 并查集合并区间
发现这种合并区间的题目还可以这么玩 给你n段时间 然后问没被占用的时间是多少 题目所给的区间是右开的导致我wa 好多人5e5*1440的暴力跑出来的时间居然只是我的两倍 不懂.... 所以并查集并没有 ...
- 移动端:active伪类无效的解决方法
:active伪类常用于设定点击状态下或其他被激活状态下一个链接的样式.最常用于锚点<a href="#">这种情况,一般主流浏览器下也支持其他元素,如button等. ...
- ZendStudio 常用快捷键大全
应用场景 快捷键 功能 查看快捷键 ctrl+shift+l 显示所有快捷键列表 查看和修改快捷键 打开Window->Preferences->General->keys 修改 ...