一.Linked List Cycle
Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium

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;
}
};
Next challenges: (M) Linked List Cycle II
 
二.Linked List Cycle II
Total Accepted: 61662 Total Submissions: 196038 Difficulty: Medium

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的更多相关文章

  1. [算法][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 ...

  2. 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 ...

  3. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. [Linked List]Intersection of Two Linked Lists

    Total Accepted: 53721 Total Submissions: 180705 Difficulty: Easy Write a program to find the node at ...

  9. Data Structure Linked List: Merge Sort for Linked Lists

    http://www.geeksforgeeks.org/merge-sort-for-linked-list/ #include <iostream> #include <vect ...

随机推荐

  1. poj 3744 矩阵 高斯消元

    着实被批评了一下,自己的数论确实太烂了. 题意:一条路上,有n个炸弹,给出每个炸弹的位置,一次走一步的概率是p,走两步的概率是1-p.求安全走完的概率. 定义dp[i] = dp[i-1]*p + d ...

  2. python面向对象(下)

    继承 继承描述了基类的属性如何"遗传"给派生类.一个子类可以继承它的基类的任何属性,不管是数据属性还是方法.创建子类的语法看起来与普通(新式)类没有区别,一个类名,后跟一个或多个需 ...

  3. j2ee基础

    1.tomcat端口被占用解决方式 使用netstat -anb查看哪个进程占用,禁止掉 修改tomacat使用的端口,在配置文件conf/server.xml 2.web app目录 3.如何建立虚 ...

  4. webpy + nginx + fastcgi 构建python应用

    1.准备环境 CentOs  6.3 nginx-1.4.2.tar.gz            http://nginx.org/download/nginx-1.4.2.tar.gz openss ...

  5. DOMDocument类文件

    http://php.net/manual/en/class.domdocument.php <?php $xml = <<< XML <?xml version=&qu ...

  6. php 数组 (3) reset() end() count() current() key()

    <?php /* count()统计数组中元素的个数 reset() 把数组内部指针移动到数组第一个元素,并返回元素值 end() 把数组内部指针移动到数组最后一个元素,并返回元素值 prev( ...

  7. 入门指引 - PHP手册笔记

    曾经简单的学习过PHP,看的是<PHP和MySQL Web开发>,还有万能的搜索引擎的帮助.这次准备系统的学习一下,参考资料是PHP Manual. PHP能做什么 PHP主要用于服务端的 ...

  8. PHP文件缓存类

    <?php /** * @desc 文件缓存 */ class Cache{ const C_FILE = '/Runtime/'; private $dir = ''; const EXT = ...

  9. git安装及使用简介

    从源代码安装 有人觉得从源码安装 Git 更实用,因为你能得到最新的版本. 二进制安装程序倾向于有一些滞后,当然近几年 Git 已经成熟,这个差异不再显著. 如果你想从源码安装 Git,需要安装 Gi ...

  10. C语言基础05

    二维数组的定义: 数据类型 数组名称 [ 常量表达式1 ] [ 常量表达式2 ] = {.....} int a[ 2 ][ 3 ] ={ {4,5,6}, {7,8,0},   //或者{7} 后面 ...