Javascript recursive solution


  • 0
    I
    var __special__ = {}
    var detectCycle = function(head) {
        if(head === null) return null;
        
        function helper(head, headnext){
            // console.log(head)
            if(head === null){
                return null
            }
            if (headnext === __special__){
                return head;
            }
            if (headnext === null){
                return null;
            }
            head.next = __special__
            var result = helper(headnext, headnext.next);
            head.next = headnext;
            if(head === result){
                return head;
            }else{
                return result;
            }
            // return
        }
        return helper(head, head.next)
    };
    

Log in to reply
 

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