一、     题目

给定一个链表。确定它是否有一个环。不使用额外的空间?

二、     分析

1. 空链表不成环

2. 一个节点自环

3. 一条链表完整成环

思路:使用两个指针,一个每次往前走2步,一个每次往前走1步。两指针一定会相遇,假设两个指针相遇,即说明链表有环存在。时间复杂度为O(N),空间复杂度为O(1)。

/**
* 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;
if(head->next==head) return true;
ListNode* node1=head->next;
ListNode* node2=head->next->next; while(node1!=NULL&&node2!=NULL){
node2=node2->next;
if(node2==NULL) break;
node2=node2->next;
node1=node1->next;
if(node1==node2) break;
}
return node1==node2;
}
}; /**
* 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* node=head->next; while(node!=NULL&&node->next!=NULL){
//一个每次往前走2步,一个每次往前走1步。两个相遇,
//即链表有环。时间复杂度为O(N)。空间复杂度为O(1)。 if(node==head||node->next==head) return true;
node=node->next->next;
head=head->next;
}
return false;
}
}; /**
* 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) {
struct ListNode* fast = head;
struct ListNode* slow = head; while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
slow = slow->next; if (fast == slow)
return true;
} return false;
}
};

Leetcode:linked_list_cycle的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. [React] React Router: Nested Routes

    Since react-router routes are components, creating nested routes is as simple as making one route a ...

  2. MVC 5.0 之奇葩错误-<类型“ASP._Page__ViewStart_cshtml”不从“System.Web.WebPages.StartPage”继承>

    在实际项目中,我们通常添加MVC项目会先添加一个MVC Empty 的项目,然后需要什么在往里面添加. 但是Empty项目里面只有一个路由注册,而且没有_ViewStart.cshtml文件需要自己添 ...

  3. Codeforces Round #279 (Div. 2)f

    树形最大上升子序列 这里面的上生子序列logn的地方能当模板使  good #include<iostream> #include<string.h> #include< ...

  4. hdu5362 Just A String(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Just A String Time Limit: 2000/1000 MS (J ...

  5. C++程序设计教程学习(1)-第一部分 编程基础

    第一章 概述 C++到底难不难学?没有学不会的事情 1.1 程序设计语言 语言 编程语言 人和计算机交流的工具,群体扩大,人人间交流过程描述与信息表达的工具 机器语言,汇编语言,高级语言 1.2 C+ ...

  6. DHTML【2】--HTML

    通过题目,大家已经明确知道,从这一节开始介绍DHTML中的最基础的部分HTML,对于HTML等概念上一节已经做了概述,这一节不再赘余.在学习HTML之前,先告诉大家一个好消息,HTML不难,比C++. ...

  7. Hadoop学习历程(四、运行一个真正的MapReduce程序)

    上次的程序只是操作文件系统,本次运行一个真正的MapReduce程序. 运行的是官方提供的例子程序wordcount,这个例子类似其他程序的hello world. 1. 首先确认启动的正常:运行 s ...

  8. Android热补丁动态修复

    1.前言 由于公司项目中使用到热修复技术,之前对这块技术知之甚少,所以有时间去学习了解了一下. 2.学习资源 2.1 热修复介绍 还是鸿洋老师的精彩讲解,中间引用了Andorid dex分包方案和QQ ...

  9. jQuery插件autoComplete使用

    安装/需要引入的文件 <script type="text/javascript" src="../js/jquery-1.8.3.min.js.js"& ...

  10. Hibernate学习笔记--映射配置文件详解

    参考资料: http://blog.163.com/hzd_love/blog/static/13199988120108265317988/ http://www.cnblogs.com/often ...