一、     题目

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

二、     分析

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. Web应用程序项目XXXX已配置为使用IIS。无法访问IIS元数据库。您没有足够的特权访问计算机上的IIS网站

    问题:Windows8下直接使用VS打开项目,出现问题:XXXX已配置为使用IIS.无法访问IIS元数据库.您没有足够的特权访问计算机上的IIS网站.解决:1.以“管理员权限”运行VS,在VS菜单打开 ...

  2. JS笔记-常见函数与问题

    1.请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. Array.prototype.distinct = functi ...

  3. sqlserver获取当前id的前一条数据和后一条数据

    一.条件字段为数值的情况   select * from tb where id=@id; --当前记录   select top 1 * from tb where id>@id order  ...

  4. Lesson 1: What is design? Why is it important?

    Week 2: What is design? Why is it important? Article 1: Startups, this is how design works. It's a s ...

  5. arraylist的使用

    ArraylistDemo package cn.stat.p6.arraylist.demo; import java.util.ArrayList; import java.util.Iterat ...

  6. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  7. c#解析XML和JSON

    http://guwei4037.blog.51cto.com/3052247/1344190  

  8. js学习笔记——数组方法

    join() 把数组中所有元素转化为字符串并连接起来,并返回该字符串, var arr=[1,2,3]; var str=arr.join("#"); //str="1# ...

  9. Clover周报模块 -- 开发总结

    2014年7月8日 00:16:05 一.切图 这次开发,切图花了不少时间,样式是用scss写的,第一次用,不过用着用着就发现它的强大,层级.作用域.重用等都非常的方便,还有考拉神器,用着真是爽!不过 ...

  10. DIV 遮挡问题总结

    1.DIV被Silverlight遮挡, 加入windowless参数即可. <object id=”silverlight” data=”data:application/x-silverli ...