[Linked List]Linked List Cycle,Linked List Cycle II
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 *first =head;
ListNode *second = head;
while(first && second)
{
first = first->next;
second = second->next;
if(second){
second = second->next;
}
if(first==second){
return true;
}
}
return false;
}
};
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Note: Do not modify the linked list.
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:
ListNode *detectCycle(ListNode *head) {
ListNode* slow=head,*fast=head;
bool has_cycle = false;
while(slow && fast){
slow = slow->next;
fast = fast->next;
if(fast){
fast=fast->next;
}
if(slow == fast && slow){
has_cycle = true;
break;
}
}
ListNode* p = has_cycle ? head:NULL;
while(has_cycle && p!=slow){
p = p->next;
slow = slow->next;
}
return p;
}
};
[Linked List]Linked List Cycle,Linked List Cycle II的更多相关文章
- [算法][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 ...
- 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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- Linked List Cycle && 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 && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- 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. Follo ...
- [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]Intersection of Two Linked Lists
Total Accepted: 53721 Total Submissions: 180705 Difficulty: Easy Write a program to find the node at ...
- Data Structure Linked List: Merge Sort for Linked Lists
http://www.geeksforgeeks.org/merge-sort-for-linked-list/ #include <iostream> #include <vect ...
随机推荐
- Struct和Class的区别
转载至:http://blog.csdn.net/yuliu0552/article/details/6717915 C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数 ...
- linux学习笔记之shell
本文参考:shell脚本学习指南 本文阅读前提为:知道shell指令,但不知道如何完成一个自动化的shell脚本. 因为编辑本文时,作者也是一个新手.所以,在一些理论上,可能存在错误.如果存在错误,希 ...
- How can I get the logical valume by the datafile names and ASM disks?
Q:We use asmlib to create ASM disk in Oracle rac 11.2.0.3, and how can I get the logical valume by t ...
- SQL Server 查看当前活动的锁
第一步: 要查看活动中的锁,如果日前根本就没有活动中的锁怎么办,还好我会自己做一把. begin tran select * from dbo.Nums with(ta ...
- 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'tb_MyInvoices' 中的标识列插入显式值
默认情况下,IDENTITY_INSER就是off 这种情况下,你写insert 语句时,identity栏位,不要写值,系统会自动帮你写入. 举例说明: ,),dt datetime,pay int ...
- MDX基础
第一章 看了本书的第一章,总体一个印象,废话真多.话不多说:整理书中知识点,实践出真理! 知识点:MDX语法:简单的函数介绍; 首先语法网上流传的很多,读者应该具备cube(多维数据集)的知识基础,我 ...
- Inno Setup 网页显示插件 webctrl (V2.1 版本)
原文 http://restools.hanzify.org/article.asp?id=90 Inno Setup网页显示插件 webctrl能够显示所有 IE 中能够显示的东西. 引用内容 ; ...
- JVM调优的几种策略(转)
JVM参数调优是一个很头痛的问题,可能和应用有关系,别人说可以的对自己不一定管用.下面是本人一些JVM调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2 ...
- haproxy image跳转 haproxy匹配 匹配到了就停止,不会继续往下匹配
<pre name="code" class="html">/***第一种 nginx 配置: location / { root /var/www ...
- linux下自动同步internet时间
linux下很简单直接一句即可: ntpdate time.nist.gov ntp后面参数为internet时间服务器url或ip即可. 但是ntpdate命令需要root特权,如果做成自动运行每次 ...