Advertisement
Guest User

aaa

a guest
Feb 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. /**
  2.  * Definition for singly-linked list.
  3.  * class ListNode {
  4.  *     public int val;
  5.  *     public ListNode next;
  6.  *     ListNode(int x) { val = x; next = null; }
  7.  * }
  8.  */
  9. public class Solution {
  10.     public ListNode detectCycle(ListNode a) {
  11.         if(a == null){
  12.             return null;
  13.         }
  14.        
  15.         if(a.next == null){
  16.             return null;
  17.         }
  18.        
  19.         ListNode walker = a;
  20.         ListNode runner = walker.next;
  21.        // int count = 0;
  22.        
  23.         while(walker.next != null && runner.next.next != null){
  24.             if(runner == walker || runner.next == walker){
  25.                 return walker;
  26.             }
  27.            
  28.             walker = walker.next;
  29.             runner = runner.next.next;
  30.            // count++;
  31.            
  32.            // if(walker == runner){
  33.            //     if(count % 2 == 0){
  34.            //         return walker;
  35.            //     } else{
  36.            //         return walker.next;
  37.            //     }
  38.            // }
  39.         }
  40.        
  41.         return null;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement