141. Linked List Cycle【easy】

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

Follow up:
Can you solve it without using extra space?

解法一:

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

经典的快慢指针思想

141. Linked List Cycle【easy】的更多相关文章

  1. 141. Linked List Cycle【Easy】【判断链表是否存在环】

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  2. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  3. 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】

    Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...

  4. 102. Linked List Cycle【medium】

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

  5. LeetCode--LinkedList--141.Linked List Cycle(Easy)

    141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...

  6. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  7. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  8. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  9. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

随机推荐

  1. [BZOJ 1499] 瑰丽华尔兹

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 Solution : 能立即发现这是和动态规划相关的题目 令f[t][i][j]表 ...

  2. 【分块】【链表】bzoj2738 矩阵乘法

    http://www.cnblogs.com/jianglangcaijin/p/3460012.html 首先将矩阵的数字排序.设置size,每次将size个数字插入.插入时,我们用h[i][j]记 ...

  3. Problem A: 调用函数,求三个数中最大数

    #include<stdio.h> int max(int a,int b,int c); int main() { int a,b,c; while(scanf("%d %d ...

  4. 在iPhone上取消APP订阅

    1.在iPhone上打开APP Store,在精品推荐那个界面滚动到底部. 2.点击Apple ID那一栏,如图: 3.点击查看Apple ID,如图: 4.点击管理,如图: 5.关掉此选项,如图: ...

  5. 看懂ios命名规则

    http://liangrui.blog.51cto.com/1510945/509289/ http://daniellee520.blog.51cto.com/372529/229615

  6. 《新一代视频压缩码标准-H.264_AVC》读书笔记1

    摘要 第一章 绪论 正文 1.一般而言,视频信号信息量大,传输网络所需要的带宽相对较宽.例如,一路可视电话或会议电视信号,由于其活动内容较少,所需带宽较窄,但要达到良好质量,不压缩约需若干 Mbps, ...

  7. RedisTemplate SerializationFailedException: Failed to deserialize payload 异常解决

    问题描述: 使用RedisTemplate(spring-data-redis )进行redis操作的封装 , 现有一个incr的key , 当调用incr后返回值一切正常, 当对此key进行get调 ...

  8. SQL Server 笔试题总结

    1:编写Sql语句,查询id重复3次以上的条目,表为Pram(id,name) 先看建立的表: SQL语句: 直接使用一个子查询即可 select * from Pram where id in(se ...

  9. C#远程获取图片文件流的方法【很通用】

    因为之前写的代码,也能获取到图片流信息,但是会是凌乱的线条,后百度得这个方法,必须记录一下 C# try { WebRequest myrequest = WebRequest.Create(Http ...

  10. 建立DB-LINK和建立视图

    在系统数据通信间经常会有数据库的数据直接引用,使用视图VIEW的方式实现.视图调用通常会有两种情况,一种是同一数据库的视图,一种是跨数据库的视图. 在同一数据库地址不同用户下,不过不同的用户视图调用需 ...