【Leetcode】Linked List Cycle
来源:互联网 发布:java web程序开发入门 编辑:程序博客网 时间:2024/05/18 01:26
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
这个问题也比较简单,我就直接贴出解题部分的代码了~
问题的核心在于设一个快指针和一个慢指针,如果“有一天”慢的追上了快的,说明中间一定有环儿~
Public class LLCycle {/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */class ListNode{int val;ListNode next;ListNode(int x){val = x;next=null;}}<span style="white-space:pre"></span>//以下为解题部分 public boolean hasCycle(ListNode head) { if(head==null||head.next==null)return true; ListNode slow=head; ListNode fast=head; while(true){ if(fast==null||fast.next==null)return true; slow=slow.next; fast=fast.next.next; if(fast==slow)return false; } }}
0 0
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- [LeetCode]Linked List Cycle
- LeetCode | Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode: Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode] - Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode]Linked List Cycle
- 【LeetCode】Linked List Cycle
- Linked List Cycle - LeetCode
- 【LeetCode】Linked List Cycle
- Linked List Cycle -- LeetCode
- 文章标题尊重原创,只有原创和翻译文章才能推荐到首页
- CMFCToolBar的简单使用
- Java中线程等待(同步)的五种方法
- astyle
- 出识指针9(容易误解的指针)
- 【Leetcode】Linked List Cycle
- HTML+css学习(1)
- Remove Duplicates from Sorted Array II
- 最近搭建HBase集群时遇到了一个问题,快把我逼疯啦,请各位大神帮我看看该怎么改啊!!!!!
- python基础三——对象
- [小说] 牧羊少年奇幻之旅(格式化之后)
- SEO优化
- HTML5定稿了,为什么原生App世界将被颠覆
- 关于kriging算法的结构分析