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. AtCoder - 2568 Lotus Leaves

    Problem Statement There is a pond with a rectangular shape. The pond is divided into a grid with H r ...

  2. [CF935F]Fafa and Array

    法法round(雾 题意:给一个序列$a_{1\cdots n}$,定义$\begin{align*}f=\sum\limits_{i=1}^{n-1}\left|a_i-a_{i+1}\right| ...

  3. [CF607D]Power Tree

    题目大意: 一棵树初始只有一个编号为$1$的权值为$w_1$的根.$q(q\le2\times10^5)$次操作,每次可以给出$v,w(w<10^9)$,新建一个结点作为$v$的子结点,权值为$ ...

  4. 【MySQL笔记】数据操纵语言DML

    1.数据插入   INSERT INTO table_name (列1, 列2,...) VALUES(值1, 值2,....),(第二条),(第三条)...   注: 1)如果表中的每一列均有数据插 ...

  5. 【R笔记】R语言中的字符串处理函数

    内容概览 尽管R是一门以数值向量和矩阵为核心的统计语言,但字符串同样极为重要.从医疗研究数据里的出生日期到文本挖掘的应用,字符串数据在R程序中使用的频率非常高.R语言提供了很多字符串操作函数,本文仅简 ...

  6. SonarQube分析报告无法上传的问题

    '); 由于SonarQube5.6 api/ce/submit 接口报以下异常,导致jenkins构建结果显示为失败~: Caused by: java.lang.NullPointerExcept ...

  7. 使用hsdis查看jit生成的汇编代码

     http://blog.csdn.net/unei66/article/details/26477629 JVM 有 HotSpot引擎可以对热代码路径进行有效的 JIT优化,大幅度提升计算密集代码 ...

  8. 取代Promise的Generator生成器函数

    接触过Ajax请求的会遇到过异步调用的问题,为了保证调用顺序的正确性,一般我们会在回调函数中调用,也有用到一些新的解决方案如Promise相关的技术. 在异步编程中,还有一种常用的解决方案,它就是Ge ...

  9. Cesium 事件

    1.相机事件(移动开始.移动结束等等) viewer.scene.camera.moveEnd.addEventListener(function(){ }): 2.鼠标事件(单击.移动.右键等) v ...

  10. IIS 日志

    查看工具: Log Parser + Log Parser Studio http://www.microsoft.com/en-us/download/details.aspx?displaylan ...