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

Follow up:

Can you solve it without using extra space?

原题链接:https://oj.leetcode.com/problems/linked-list-cycle/

题目:给定一个链表。推断它是否有环。

继续:

你能不用额外的空间解决吗?

思路:使用两个指针fast,slow,fast每次向前走两步,slow每次向前走一步。假设二者相应的节点相等。即存在环。

	public boolean hasCycle(ListNode head) {
ListNode fast = head,slow = head;
if(head == null || head.next == null)
return false;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
return true;
}
return false;
} // Definition for singly-linked list.
class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
next = null;
}
}

LeetCode——Linked List Cycle的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. [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. [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 ...

  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. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. LeetCode: Linked List Cycle 解题报告

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

  8. LeetCode Linked List Cycle 解答程序

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

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

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

  10. [LeetCode] Linked List Cycle II, Solution

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

随机推荐

  1. 移动端日期控件 mobiscroll

    Mobiscroll是一个用于触摸设备(Android phones, iPhone, iPad, Galaxy Tab)的日期和时间选择器jQuery插件.可以让用户很方便的只需要滑动数字既可以选择 ...

  2. 配置并学习微信JS-SDK(3)—菜单接口

    1.设置菜单 //2.批量显示菜单项接口 wx.showMenuItems({   menuList: [     'menuItem:profile', //查看公众号     'menuItem: ...

  3. 批处理协同blat自动发邮件

    Blat - A Windows (32 & 64 bit) command line SMTP mailer. Use it to automatically eMail logs, the ...

  4. 定制textField

    2014-08-05 11:00 447人阅读 评论(0) 收藏 举报  分类: IOS开发笔记(248)  版权声明:本CSDN博客所有文章不会即时更新,请关注个人博客:http://www.hua ...

  5. PHP-mac下的配置及运行

    Here's another option, from the guys from liip, here. This is a PHP package that comes pre-built for ...

  6. bzoj 1501: [NOI2005]智慧珠游戏 Dancing Link

    1501: [NOI2005]智慧珠游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 122[Submit][Status] ...

  7. prototype.js 源码解读(02)

    如果你想研究一些比较大型的js框架的源码的话,本人建议你从其最初的版本开始研读,因为最初的版本东西少,易于研究,而后的版本基本都是在其基础上不断扩充罢了,所以,接下来我不准备完全解读prototype ...

  8. Windows Azure 配置SSTP

    方法參考下面文章的步驟. 这个成功.http://freevpnba.com/windows-azure-sstp-vpn/ 这个没成功,不知道为什么.http://diaosbook.com/pos ...

  9. Unity3D 命令行参数

    Unity3D 命令行参数 @by 广州小龙                                              unity ios开发群:63438968 Typically, ...

  10. 吉哥系列故事——完美队形II

    hdu4513:http://acm.hdu.edu.cn/showproblem.php?pid=4513 题意:给以一个序列,然后让你求一个最长回文序列的长度,这个序列的从左到最中间那个数是不降的 ...