Javascript recursive


  • 0
    R
    var reorderList = function(head) {
        var len = getLength(head);
        reorder(head, 0, len-1);
    };
    function getLength (head){
        var i= 0;
        var node = head;
        while(node){
            node=node.next;
            i++;
        }
        return i;
    }
    
    function reorder(node, start,end ){
        var temp, temp1;
        if(end - start  === 1 ){
            temp = node.next.next;
            node.next.next  = null;
            return [node, temp];  
        }
        else if(start === end){
            temp = node.next;
            node.next= null;
            return [node, temp];
        }
        if(node){
         temp = reorder(node.next, start+1,end-1);
         temp1 = temp[1].next;
         node.next = temp[1];
         temp[1].next = temp[0];
         return [node, temp1];
        }
        
    }
    

Log in to reply
 

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