52-Linked List Cycle
- 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的更多相关文章
- [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] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
- [算法][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 —— 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 ...
- 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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 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 ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 3月2日 Linked List Cycle
今天星期天,准备好周一的PPT内容,再回来做题,以后考虑周末做一个APP或者微信帐号玩吧. 回到题目, Linked List Cycle,一个检查单项链表是否有环路的问题. 题目周五的时候就简单做过 ...
随机推荐
- Noip模拟18 2021.7.17 (文化课专场)
T1 导弹袭击(数学) 显然,我们要找到最优的A,B使得一组a,b优于其他组那么可以列出: $\frac{A}{a_i}+\frac{B}{b_i}<\frac{A}{a_j}+\frac{B} ...
- [调试笔记] 10.8模拟赛11 T4 甜圈
这题正解线段树维护哈希,同机房神犇已经讲的很明白了.这里只说sbwzx在调试的时候犯的sb错误. 1.关于pushdown和update 众所周知,sbwzx一写带lazy的线段树,就必在pushdo ...
- Exynos4412 中断处理流程详解
Linux 中,当外设触发中断后,大体处理流程如下: a -- 具体CPU architecture相关的模块会进行现场保护,然后调用machine driver对应的中断处理handler; b - ...
- Luogu P1850 [NOIp2016提高组]换教室 | 期望dp
题目链接 思路: <1>概率与期望期望=情况①的值*情况①的概率+情况②的值*情况②的概率+--+情况n的值*情况n的概率举个例子,抛一个骰子,每一面朝上的概率都是1/6,则这一个骰子落地 ...
- iptables 原理及应用
转自:iptables 原理及应用 iptables是一个Linux下优秀的nat+防火墙工具,我使用该工具以较低配置的传统pc配置了一个灵活强劲的防火墙+nat系统,小有心得,看了网上也有很多这方面 ...
- [源码解析] PyTorch 分布式(1)------历史和概述
[源码解析] PyTorch 分布式(1)------历史和概述 目录 [源码解析] PyTorch 分布式(1)------历史和概述 0x00 摘要 0x01 PyTorch分布式的历史 1.1 ...
- 深入剖析 RocketMQ 源码 - 消息存储模块
一.简介 RocketMQ 是阿里巴巴开源的分布式消息中间件,它借鉴了 Kafka 实现,支持消息订阅与发布.顺序消息.事务消息.定时消息.消息回溯.死信队列等功能.RocketMQ 架构上主要分为四 ...
- Vue 之 Mixins (混入)的使用
是什么 混入 (mixins): 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被合并到组件本身,也就是说父组件调用混入 ...
- Linux环境下安装、配置Redis
linux下安装redis 官网下载链接:https://redis.io/download 安装 下载redis压缩包 1.选择Stable(5.0)下的Download 5.0.0 链接进行下载 ...
- idea关联数据库
时区错误解决方法