Advertisement
md5kafka

Untitled

Oct 3rd, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. /**
  2.  * Definition for singly-linked list.
  3.  * class ListNode {
  4.  *     int val;
  5.  *     ListNode next;
  6.  *     ListNode(int x) {
  7.  *         val = x;
  8.  *         next = null;
  9.  *     }
  10.  * }
  11.  */
  12. public class Solution {
  13.     public boolean hasCycle(ListNode head) {
  14.         if(head == null) {
  15.             return false;
  16.         }
  17.         ListNode seen = new ListNode();
  18.         while(head.next != null) {
  19.             if(head.next == seen) {
  20.                 return true;
  21.             }
  22.             ListNode p2 = head.next;
  23.             head.next = seen;
  24.             head = p2;
  25.         }
  26.         return false;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement