一种方法是用set存储出现过的指针,重复出现时就是有环:

 class Solution {
public:
bool hasCycle(ListNode *head) {
set<ListNode*> st;
ListNode *p = head;
while(p) {
if (st.find(p) != st.end())
return true;
st.insert(p);
p = p->next;
}
return false;
}
};

另一种方法就是快慢指针,快指针一次走两步,慢指针一次走一步。无环时快指针会走向空指针,有环时快指针会赶上慢指针。

 class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast, *slow;
fast = slow = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (fast == slow)
return true;
}
return false;
}
};

leetcode 141、Linked list cycle的更多相关文章

  1. [LeetCode] 141&142 Linked List Cycle I & II

    Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...

  2. leetcode 141 142. Linked List Cycle

    题目描述: 不用辅助空间判断,链表中是否有环 /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  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. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  5. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  6. 【LeetCode】142. Linked List Cycle II (2 solutions)

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. 【LeetCode练习题】Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  8. &lt;LeetCode OJ&gt; 141 / 142 Linked List Cycle(I / II)

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  9. LeetCode算法题-Linked List Cycle(Java实现)

    这是悦乐书的第176次更新,第178篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第35题(顺位题号是141).给定一个链表,确定它是否有一个循环. 本次解题使用的开发工 ...

随机推荐

  1. Flask&&人工智能AI -- 8 HTML5+ 初识,HBuilder,夜神模拟器,Webview

    昨日内容回顾 1.增删改查: 增: db.collections.insert({a:1}) // 官方不推荐了 db.collections.insertMany([{a:1},{b:1}]) in ...

  2. http协议&接口规范&接口测试入门

    http协议 请求: 请求行:请求方法.url(协议名://ip;端口/工程名/资源路径).协议版本 请求头 :键值对 请求正文 响应: 响应行:协议版本.响应状态码.响应状态码描述 响应头 :键值对 ...

  3. caffe 日志保存以及matlab绘制方法(windows以及ubuntu下)

    caffe 用matlab解析日志画loss和accuracy clc; clear; % load the log file of caffe model fid = fopen('log-prev ...

  4. NET Core 不错教程***************

    Twinhead 当前标签: .Net Core   RabbitMQ教程 Twinhead 2019-01-26 20:02 阅读:5 评论:0     缓存击穿.缓存穿透和缓存雪崩 Twinhea ...

  5. Jenkins安全配置详解

    一.进入安全配置界面 首页后点击进入系统管理(Manage Jenkins) ——下拉下方看到安全配置(Configure Global Security) ——进入安全配置界面 二,详解安全配置的选 ...

  6. grunt 安装使用(一)

    grunt 依赖nodejs,所有在使用前确保你安装了nodejs,然后开始执行grunt命令. .安装node nodejs安装教程 安装完成后在命令行,执行命令: node  -v 出现版本信息, ...

  7. Unity Resources.Load

    GameObject bulletPrefab; void Start() {   bulletPrefab = Resources.Load("bulletPrefab")  a ...

  8. [转]AngularJS 使用 UI Router 实现表单向导

    本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...

  9. Murano Weekly Meeting 2015.09.08

    Meeting time: 2015.September.8th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting su ...

  10. Mongodb installation & userguide

    1.Mongodb Installation in Ubuntu (1) Download from: https://www.mongodb.org/downloads File: mongodb- ...