LeetCode-Easy刷题(32) Linked List Cycle

Posted 当以乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Easy刷题(32) Linked List Cycle相关的知识,希望对你有一定的参考价值。

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?


给定一个链表,确定它是否有一个循环。  你能在不使用额外空间的情况下解决它吗?



 //两个速度指针
    public boolean hasCycle(ListNode head) 

       if(head ==null)
           return false;
       
       ListNode walker = head;
       ListNode runner = head;
       while(runner!=null && runner.next!=null)

           walker = walker.next;
           runner = runner.next.next;
           if(walker == runner)
               return true;
           
       
       return false;
    


以上是关于LeetCode-Easy刷题(32) Linked List Cycle的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-Easy刷题(31) Single Number

LeetCode-Easy刷题 Remove Element

LeetCode-Easy刷题(19) Same Tree

LeetCode-Easy刷题(33) Min Stack

LeetCode-Easy刷题(33) Min Stack

LeetCode-Easy刷题(26) Path Sum