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

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

题意:

给定一个链表,判断是否循环

思路:

快慢指针

若有环,则快慢指针一定会在某个节点相遇(此处省略证明)

代码:

 public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow) return true;
}
return false;
}
}

[leetcode]141. Linked List Cycle判断链表是否有环的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

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

  2. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  3. 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 ...

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

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

  5. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

  6. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  7. LeetCode 141. Linked List Cycle环形链表 (C++)

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

  8. [Leetcode] Linked list cycle 判断链表是否有环

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

  9. 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 ...

随机推荐

  1. 送人玫瑰,手留余香——2015年技术分享交流小结

    飞测说:分享让我们更加团结,交流让我们更加凝聚,送人玫瑰,手留余香,更多分享交流也让自己成长的更加完善,2015年已经过去了好几个月,今天刚好整理了下我们科大讯飞武汉测试团队技术分享交流的这块,顺便做 ...

  2. 一个Self Taught Learning的简单例子

    idea: Concretely, for each example in the the labeled training dataset xl, we forward propagate the ...

  3. c++ istringstream的用法

    一.测试代码 istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流.这样就实现了字符串的空格切割. #include<iostream ...

  4. LG4717 【模板】快速沃尔什变换

    题意 题目描述 给定长度为\(2^n\)两个序列\(A,B\),设\(C_i=\sum_{j\oplus k}A_jB_k\)分别当\(\oplus\)是or,and,xor时求出C 输入输出格式 输 ...

  5. 图解VS2005之单元测试

    据说VS2005里即提供了测试功能,可是对于像我或者我们这样的开发人或团队真还没有进化到用测试这块.一直以来都是手工测试或等到用户发现问题.今天在网上找了一个介绍单元测试的WORD文档,按里面说的做了 ...

  6. VsCode中运行nodeJs代码的简单方法

    VsCode安装包默认内置的node debug插件需要配置工程调试运行文件才能正常运行,对于想要运行一个简单的js文件或者就是一段js代码时比较麻烦,为此可以安装Code Runner插件 安装完后 ...

  7. [C++/Python] 如何在Python中使用一个DLL? (Windows环境)

    开发环境VS2012, WIN7 64. 首先生成的DLL大致如下: .h文件 #ifdef CVINPYTHON_EXPORTS #define CVINPYTHON_API __declspec( ...

  8. winodws同步时间命令

    首先,你应该判断你的两台域控制器,哪一台担任PDC角色(默认的域内权威的时间服务源). 判断方法很简单,单击“开始”,单击“运行”,键入dsa.msc,然后点确定.这时会打开“Active Direc ...

  9. LOJ 6485 LJJ 学二项式定理——单位根反演

    题目:https://loj.ac/problem/6485 \( \sum\limits_{k=0}^{3}\sum\limits_{i=0}^{n}C_{n}^{i}s^{i}a_{k}[4|(i ...

  10. TS流解析 一

    一 从TS流开始 数字电视机顶盒接收到的是一段段的码流,我们称之为TS(Transport Stream,传输流),每个TS流都携带一些信息,如Video.Audio以及我们需要学习的PAT.PMT等 ...