Leetcode – Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Analysis
If we have 2 pointers - fast and slow. It is guaranteed that the fast one will meet the slow one if there exists a circle.
The problem can be demonstrated in the following diagram:
Java Solution
public class Solution { public boolean hasCycle(ListNode head) { ListNode fast = head; ListNode slow = head; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; } } |
<pre><code> String foo = "bar"; </code></pre>
-
গোলাম মোস্তফা রুমি
-
Prakash Jha
-
Marina
-
Tayyab
-
Vani
-
Tayyab
-
tm
-
Carlos Gomez
-
Tayyab
-
ryanlr
-
ryanlr
-
dd
-
shadowlou