【LeetCode】141. Linked List Cycle
题目:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
提示:
首先,题目中要求'without using extra space',指的是空间复杂度必须控制在O(1)内。
因此可以创建两个变量,先同时指向head,然后每一轮循环中,令其中一个变量沿链表向前“走”两步,另一个走“一步”,这样的话每一个循环后他们两者的距离差会+1。
如果链表里有回路的话,那么这个回路相当于是一个取模的操作,当两者的距离差等于回路的长度时,其实就意味着已经相遇了。因此这两个变量终会在某一步相遇,这时候就能判断出有回路了。
反之如果没有回路,那么“走”的较快的那个变量会率先走到NULL,这时候就可以判定没有回路。
代码:
/**
* 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) {
ListNode *fast = head;
ListNode *slow = head;
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow)
return true;
}
return false;
}
};
【LeetCode】141. Linked List Cycle的更多相关文章
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【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 ...
- 【一天一道LeetCode】#141. Linked List Cycle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 【easy】141. Linked List Cycle
非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- [玩耍]C++控制台扫雷
其实是大一还不会GUI时闲着无聊写的.都是硬编码,也不支持自定义棋盘大小,现在看看这代码惨不忍睹.下载地址:http://download.csdn.net/download/xienaoban/98 ...
- 实现ThreadFactory接口生成自定义的线程给Fork/Join框架
Fork/Join框架是Java7中最有趣的特征之一.它是Executor和ExecutorService接口的一个实现,允许你执行Callable和Runnable任务而不用管理这些执行线程.这个执 ...
- 每天一道Java题[3]
问题 为什么在重写equals()方法的同时,必须重写hashCode()方法? 解答 在<每天一道Java题[2]>中,已经对hashCode()能否判断两个对象是否相等做出了解释.eq ...
- Ajax,纯Js+Jquery
AJAX:Asynchronous Javascript and xml 异步,Js和Xml 交互式网页开发 不刷新页面,与服务器交互 详情请参照Jquery工具指南用在浏览器端的技术,无刷新,通过X ...
- URI结构
[scheme:][//host:port][path][?query][#fragment] path:从端口后第一个/开始,可以有多个,每个用/连接. query:从第一个?开始,至行尾或#结束. ...
- c++堆与栈的简单认识
堆: 操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,会遍历该链表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表中删 除,并将该结点的空间分配给程序,另外,对于大多数 ...
- Visual Studio Code for mac
Visual Studio Code for mac 将下载文件解压拖到应用程序文件夹即可 下载地址:链接: https://pan.baidu.com/s/1geHL5f1 密码: 2fdw
- html学习笔记 - sublime text 插件安装
command + shift + p 呼出搜索界面 输入 Packge Control:Install Package 进入到插件搜索列表 Emmet -- >快速生成html标签结构 Emm ...
- 用app.net Core搞点多国语言网站
Asp.net Core 中文文档很少,你可以看英文的,不过英文的也是说的有点乱.这篇文章是干货. 1. 配置好你的WebApplication,使他可以支持国际化语言,修改文档Startup.cs ...
- java中的一些规则(菜鸟的课堂笔记)
ls 查看目录下文件 java规则 代码都定义在类中,用class定义 禁止一个源文件写两个类: 一个源文件中,只能有一个类文件是pubic: 一个源文件中如果有多个类,编译完之后会产生多个class ...