Leetcode-Linked List Cycle

来源:互联网 发布:php 多进程编程 编辑:程序博客网 时间:2024/06/05 09:01
Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

AC代码:

public class LinkedListCycle {public boolean hasCycle( ListNode head ){ListNode slowNode = head;ListNode fastNode = head;if( head == null || head.next == null ){return false;}//classic slow and fast pointer find cyclewhile( true ){if( slowNode == null || fastNode == null || fastNode.next == null ){return false;}slowNode = slowNode.next;fastNode = fastNode.next.next;if ( slowNode == fastNode ){return true;}}}


0 0
原创粉丝点击