标签:span 条件 script color list walk amp als desc
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
利用快慢指针,如果相遇则证明有环
注意边界条件: 如果只有一个node.
public class Solution { public boolean hasCycle(ListNode head) { if(head == null) return false; ListNode walker = head; ListNode runner = head; while(runner.next != null && runner.next.next != null){ walker = walker.next; runner = runner.next.next; if(walker == runner) return true; } return false; } }
标签:span 条件 script color list walk amp als desc
原文地址:http://www.cnblogs.com/zle1992/p/7569489.html