[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 ...
随机推荐
- .net web api 一
web api可以提供方便简单可靠的web服务,可以大量的用于不需要提供复杂的soap协议的环境,以简单明了的形式返回数据,在不太复杂的环境中web api可以做为wcf等重级web服务的一种可替代方 ...
- andrid中的Sqlite 数据库连接(本地版)
sqlite简介 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前 ...
- dojo.byId、dojo.query、dojo.attr
概述: dojo.byId(/*string*/id或/*DomNode*/node) 1.传入DOMNode返回传入的domNode; 2.传入id返回id为当前值的domNode dojo.que ...
- 数据库监控[Z]
--查看表锁 select * from sys.v_$sqlarea where disk_reads>100 --监控事例的等待 select event,sum(decode(wai ...
- foreach的用法(转)
JDK1.5加入的增强for和循环. foreach语句使用总结增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部变量, ...
- Java并发编程小记
1. Semaphore 信号量是一种计数器,用来保护一个或者多个共享资源的访问.如果线程要访问一个共享资源,必须先获得信号量.若内部计数器大于0,则减1,若等于0,则线程进入休眠直至计数器大于等于0 ...
- python之函数的使用
备注:本篇文章主要讲一讲函数及集合的一些常用用法: 一.首先先看下,集合(set): 集合的特点:无序.不重复(这点跟字典有点像) <1>,在需要访问集合的时候,由于集合本身是无序的,所以 ...
- cocos2d-x中的Tiled地图
cocos2d-x中的瓦片地图是通过tiledMap软件制作的,存档格式是.tmx格式.此软件的使用步骤简单总结如下: (1)制作瓦片地图 1 打开软件,软件界面如下图. 2. 新建地图(文件-> ...
- linux function
#!/bin/bash function sayHello() { #这里的{ 和它前面的)之间可以没有空格 echo "Hello $1" } sayHello 'Neeky'
- android 代码布局 记录
1.概述 android 中大部分ui 布局是用xml 进行的,但是用代码布局调整是不可避免的.自己比较喜欢写模版,有些时候子类再继承模版时,往往有一些ui上的调整,又懒得去重写一个xml,就偷懒用代 ...