Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * class ListNode {
- * public int val;
- * public ListNode next;
- * ListNode(int x) { val = x; next = null; }
- * }
- */
- public class Solution {
- public ListNode detectCycle(ListNode a) {
- if(a == null){
- return null;
- }
- if(a.next == null){
- return null;
- }
- ListNode walker = a;
- ListNode runner = walker.next;
- // int count = 0;
- while(walker.next != null && runner.next.next != null){
- if(runner == walker || runner.next == walker){
- return walker;
- }
- walker = walker.next;
- runner = runner.next.next;
- // count++;
- // if(walker == runner){
- // if(count % 2 == 0){
- // return walker;
- // } else{
- // return walker.next;
- // }
- // }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement