141. Linked List Cycle

双指针

 1     public boolean hasCycle(ListNode head) {
 2         if(head == null) {
 3             return false;
 4         }
 5         ListNode runner = head;
 6         ListNode walker = head;
 7         while(runner.next != null && runner.next.next != null) {
 8             walker = walker.next;
 9             runner = runner.next.next;
10             if(walker == runner) {
11                 return true;
12             }
13         }
14         return false;
15     }

 

posted @ 2016-06-21 06:26  warmland  阅读(80)  评论(0编辑  收藏  举报