Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 0 ms, faster than 100.00% of Java online submissions for Linked List Cycle.
- public class Solution {
- public boolean hasCycle(ListNode head) {
- if (head == null) return false;
- ListNode start = head;
- int steps = 1;
- for (;;) {
- ListNode next = start;
- for (int i = 0; i < steps; i++) {
- next = next.next;
- if (next == null) return false;
- if (next == start) return true;
- }
- start = next;
- steps *= 2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement