Linked List Cycle
2014-01-27 08:38
483 查看
Linked List Cycle
Total Accepted: 8039 TotalSubmissions: 23165My Submissions
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) return false;
ListNode slow = head, fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast) return true;
}
return false;
}
}
相关文章推荐
- Linked List Cycle
- [leetcode-141]Linked List Cycle(c)
- leetcode : Linked List Cycle II
- Linked list cycle [leetcode]
- 141. Linked List Cycle
- LeetCode 142: Linked List Cycle II
- Linked List Cycle
- leetcode Linked List Cycle II
- Linked List Cycle II(2014.2.7)
- leetcode141~Linked List Cycle
- Linked List Cycle
- leetcode 日经贴,Cpp code -Linked List Cycle II
- [刷题]Linked List Cycle
- [LeetCode]Linked List Cycle
- Linked List Cycle
- 142. Linked List Cycle II
- Leetcode:linked_list_cycle
- Linked List Cycle
- Linked List Cycle II
- 2.2.11—单链表—Linked List Cycle