Help Needed : Failing for test case [1,2]


  • 0
    P

    The below algorithm is passing for 15/16 test cases.

    It is failing for the test case : [1,2]
    Expected Output : tail connects to node index 0
    My output : tail connects to node index 1

    But when i run this test case locally my method is returning null as expected.

    public class Solution {
        public ListNode detectCycle(ListNode head) {
        if(head==null || head.next==null)
        			return null;
        		ListNode tor = head;
        		ListNode rab = head.next;
        		ListNode find = head;
        		if(rab!=tor){
        			while(tor.next!=null && rab.next!=null && rab.next.next!=null){
        				tor = tor.next;
        				rab = rab.next.next;
        				if(tor==rab){
        					tor = tor.next;
        					while(tor.next!=null && find.next!=null){
        						tor=tor.next;
        						find = find.next;
        						if(tor==find){
        							return find;
        						}
        					}
        					
        				}
        			}
        		}
        		else{
        			return head;
        		}
        		return null;
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.