leetcode第10题(linked-list-cycle)
2018-01-14 14:56
218 查看
题目:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
思路:跟上面题一样,只是不用求相遇点,第一次相遇就可以判断有环。
代码:
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. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head){ if(head == null || head.next == null){ return false; } ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next != null){ fast = fast.next.next; slow = slow.next; if(slow==fast){ return true; } } return false; } }
相关文章推荐
- [LeetCode 第10题] -- Linked List Cycle
- LeetCode:Linked List Cycle II
- LeetCode---Linked List Cycle、Linked List Cycle II解题分析
- LeetCode 141 Linked List Cycle和142 Linked List Cycle II
- 【LeetCode】Linked List Cycle
- 【LeetCode】141 Linked List Cycle (java实现)
- Leetcode: Linked List Cycle
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
- [LeetCode] Linked List Cycle II 单链表中的环之二
- LeetCode Linked List Cycle II 超时问题
- 【LeetCode】【C++】Linked list cycle 2
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- leetcode第一刷_Linked List Cycle
- LeetCode142:Linked List Cycle II
- LeetCode-Easy部分标签为LinkedList 141 Linked List Cycle
- <leetcode系列> Linked List Cycle
- 【LeetCode】Linked List Cycle II
- LeetCode Linked List Cycle
- Leetcode@python Linked List Cycle
- [LeetCode]Linked List Cycle II