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?
注意,链表循环并不是尾指针和头指针相同,可能是在中间某一段形成一个环路,所以不能只判断元素和第一个元素是否存在重合
先设置两个指针p_fast和p_slow。从头开始遍历链表,p_fast每次走两个节点,而p_slow每次走一个节点,若存在循环,这两个指针必定重合:
/**
* 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) return false; ListNode *p_fast = head;
ListNode *p_slow = head; do{
p_slow = p_slow->next;
if(p_fast != NULL)
p_fast = p_fast->next;
if(p_fast != NULL)
p_fast = p_fast->next;
else
return false;
}while(p_fast != p_slow); return true; }
};
Linked List Cycle 判断一个链表是否存在回路(循环)的更多相关文章
- [算法][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 ...
- [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 ext ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 力扣——Linked List Cycle(环形链表) python实现
题目描述: 中文: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. ...
- 【LeetCode】Linked List Cycle II(环形链表 II)
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...
- [LC]141题 Linked List Cycle (环形链表)(链表)
①中文题目 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 ...
- 20140719 找到单链表的倒数第K个节点 判断一个链表是否成为一个环形 反转
1.找到单链表的倒数第K个节点 2.判断一个单链表对否形成环形 3.单链表翻转
随机推荐
- 逆向学习-Windows消息钩取
钩子 Hook,就是钩子.偷看或截取信息时所用的手段或工具. 消息钩子 常规Windows流: 1.发生键盘输入事件时,WM_KEYDOWN消息被添加到[OS message queue]. 2.OS ...
- server端获得到client端的IP地址的格式
使用telnet,ping或其他client连接server端时,server端获得的client端的ip地址取决于client端使用的时ipv4还是ipv6地址. 例: client IPv4地址: ...
- [转] Scala 的集合类型与数组操作
[From] https://blog.csdn.net/gongxifacai_believe/article/details/81916659 版权声明:本文为博主原创文章,转载请注明出处. ht ...
- java transient 和Volatile关键字
Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...
- 在Windows Server 2008 R2(x64)上安装.NET Framework 4.5 兼谈.NET Framework 4.0 “在服务器核心角色上不受支持”含义
完成了一个服务器文件监控系统,该系统的核心是一个Windows服务,需要安装在服务器上.由于是Visual Studio 2012开发,为了保证开发的Windows服务可以运行,必须在Windows服 ...
- Ubuntu16.04 下如何安装搜狗拼音输入法【亲测有效】
Ubuntu16.04 下如何安装搜狗拼音输入法[亲测有效] 一.添加fcitx键盘输入法系统[系统默认是iBus] 1.将下载源添加至系统源: sudo add-apt-repository p ...
- django blank
null: If True, Django will store empty values as NULL in the database. Defaultis False. 如果为True,空值将会 ...
- feignClient中修改ribbon的配置
1.使用@FeignClient注解发现服务 服务提供者的controller: @RestController public class StudentController { @Autowired ...
- smarty 教程 及 常用点
1. 简单例子 有助回忆基本知识点 define("DIR",dirname(__FILE__)); require_once(DIR."/libs/Smarty.cla ...
- js实现私有变量
一.块级作用域 js中没有块级作用域的概念,可用匿名函数实现,由于匿名函数执行完一遍后,内部没有引用其变量对象的函数,其变量对象被清除,后面则引用不到其中的变量 (function(){ //块级作用 ...