1. Linked List Cycle My Submissions QuestionEditorial Solution

    Total Accepted: 102785 Total Submissions: 278248 Difficulty: Easy

    Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

思路:快慢指针

一个走一步,一个走两步

最后如果相遇则有环(为什么呢?,考虑在环上的情况,因为快指针走的快,走的是2步,一定可以追上慢指针,那你可能会想走两步可能直接略过慢指针啊,假设某个时刻快指针在慢指针前一个位置,下一次便相遇,若果在前2个呢,那么下一次便和慢指针相邻,回到之前的情况,前1和前2涵盖了数位奇偶,所以总言之一定会相遇)

更多的是考虑到特殊情况,为空,只有一个节点,只有两个节点

/**
* 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 *p_quick=head,*p_slow=head;
while(p_quick!=NULL&&p_quick->next!=NULL){
p_quick = p_quick->next->next;
p_slow = p_slow->next;
if(p_quick==p_slow)return true;
}
return false;
}
};

52-Linked List Cycle的更多相关文章

  1. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. [LeetCode] Linked List Cycle 单链表中的环

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

  3. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

  4. [算法][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 ...

  5. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

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

  6. 15. 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 solve i ...

  7. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  8. Java for LeetCode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. 【题解】【链表】【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. 3月2日 Linked List Cycle

    今天星期天,准备好周一的PPT内容,再回来做题,以后考虑周末做一个APP或者微信帐号玩吧. 回到题目, Linked List Cycle,一个检查单项链表是否有环路的问题. 题目周五的时候就简单做过 ...

随机推荐

  1. dice_game攻防世界进阶区

    dice_game XCTF 4th-QCTF-2018 前言,不得不说,虽然是个简单题但是还是要记录一下,来让自己记住这些东西. 考察的知识点是: 1.cdll_loadlibrary加载对应库使得 ...

  2. 你知道如何从单片机过渡到嵌入式linux需要经历那些吗?(这个亲身体验有效)

    就现在的行业发展来看只会单片机已经不吃香了并且在薪资待遇方面来看的话单片机的收入限制性太强可能工作很多年之后发现没有了成长空间,因此逐渐转到嵌入式Linux这个方向是越来越多的人的一个选择,那么接触了 ...

  3. 虚树 virtual-tree

    我们发现,如果一棵树中真正需要处理的点很少,而总共点数很多时,可以只处理那些需要的点,而忽略其他点. 因此我们可以根据那些需要的点构建虚树,只保留关键点. oi-wiki上对虚树的介绍 我们根据一下方 ...

  4. Vivado Synth/Place Faild但是没有给出error信息

    最近遇到一个现象,以前可以编译通过的工程,修改之后发现Synthesis编译报错,而且没有给出error信息,以前也出现过无故place 失败但是没有给出error信息的现象,查看错误日志输出文件,出 ...

  5. 20191310Lee_yellow缓冲区溢出实验

    缓冲区溢出实验 1.什么是缓冲区溢出 ​ 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据 ...

  6. JavaScript数组方法大集合

    JavaScript数组方法集合 本文总结一下js数组处理用到的所有的方法.自己做个笔记. 数组方法 concat() 合并两个或多个数组 concat()能合并两个或者多个数组,不会更改当前数组,而 ...

  7. Charles--/安装/破解/支持https抓包

     一.安装破解Charles 1.下载charles4.0.2版本,下面的jar包需要和charles版本对应 2.下载地址:https://www.cr173.com/soft/494576.htm ...

  8. 开源支持东财自动交易的简单量化交易框架pytrader

    pytrader是基于 easytrader 和 easyquotation 的量化交易框架,支持东方财富自动交易,支持进行策略回测. 开源地址:https://github.com/jadepeng ...

  9. spring笔记-MultiValueMap

    即一个键对应多个值,Spring的内部实现是LinkedMultiValueMap MultiValueMap接口 一键多值的使用场景是比较多的,在使用该数据结构之前,通常会自己定义 Map<K ...

  10. cmd 命令 导出导入oracle数据库 的 表

    原地址:https://www.cnblogs.com/mysterious-killer/p/11671741.html (防止) 导出: 不要数据的:exp username/pwd@localh ...