Do you know "JS pointer" ? I have got AC,but I don't know WHAT's wrong in my Wrong Answer


  • 0
    S

    First ,the Wrong Answer, very same with my AC answer only 4 line not same

    function reverse(head, k,last,result){
    
        var p=head,i=k;
        var ps,newHead=null;
        while((i>1)&&(p)&&(p.next)){
            i--;
            
            p=p.next;
        }
        
        if(i>1){
            if(last){
                last.next=head;
            }
            
            return [null,null,result];
        }
        debugger;
        newHead=p.next;
        
        ps=p;
        i=k;
        if(result==head){
            result=p;
        }
        var save=null;
    
        //last.next=head;
        p=head.next;
        var start=head;
        //var list=[0,12,34,56,79];
        while(i>1){
           
           
            save=p.next;
            p.next=start;
            start=p;
            p=save;
            i--;
        }
        if(last!=null){
            last.next=ps;
        }
        var rr=head;
        head=newHead;
        return [rr,head]; //not same with the AC answer
    }
    var reverseKGroup = function(head, k) {
        if((k==1)||(k==0)){
            return head;
        }
        
        if(head==null) return head;
        //check single-node list;
        var last=[null,head];
        var result=head; //not same with the AC answer
        while(head){
            //转
            
            last=reverse(last[1],k,last[0],result);//not same with the AC answer
            if(last[0]==null){//not same with the AC answer
                break;
            }
        }
        return result;//not same with the AC answer
    };
    

    //Second, My AC answer

    function reverse(head, k,last,result){
    
        var p=head,i=k;
        var ps,newHead=null;
        while((i>1)&&(p)&&(p.next)){
            i--;
            
            p=p.next;
        }
        
        if(i>1){
            if(last){
                last.next=head;
            }
            
            return [null,null,result];
        }
        debugger;
        newHead=p.next;
        
        ps=p;
        i=k;
        if(result==head){
            result=p;
        }
        var save=null;
    
        //last.next=head;
        p=head.next;
        var start=head;
        //var list=[0,12,34,56,79];
        while(i>1){
           
           
            save=p.next;
            p.next=start;
            start=p;
            p=save;
            i--;
        }
        if(last!=null){
            last.next=ps;
        }
        var rr=head;
        head=newHead;
        return [rr,head,result];
    }
    var reverseKGroup = function(head, k) {
        if((k==1)||(k==0)){
            return head;
        }
        
        if(head==null) return head;
        //check single-node list;
        var last=[null,head,head];
        while(head){
            //转
            
            last=reverse(last[1],k,last[0],last[2]);
            if(last[0]==null){
                break;
            }
        }
        return last[2];
    };

Log in to reply
 

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