Advertisement
ogv

Untitled

ogv
Nov 26th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. // Runtime: 0 ms, faster than 100.00% of Java online submissions for Linked List Cycle.
  2. public class Solution {
  3.     public boolean hasCycle(ListNode head) {
  4.         if (head == null) return false;
  5.         ListNode start = head;        
  6.        
  7.         int steps = 1;
  8.         for (;;) {
  9.             ListNode next = start;
  10.            
  11.             for (int i = 0; i < steps; i++) {
  12.                 next = next.next;
  13.                 if (next == null) return false;
  14.                 if (next == start) return true;
  15.             }
  16.            
  17.             start = next;
  18.             steps *= 2;
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement