JavaScript: Reverse array could be accepted?


  • 0
    L

    I just try with dumb method, and find this:

    var reverseList = function(head) {
        if(!head)
            return head
        var tmp = head,
            array = [],
            tmpRes;
        while(tmp !== null) {
            array.push(tmp.val);
            tmp = tmp.next
            // console.log(array)
        }
        
        return array.reverse()  // Accepted
    
        for( var i = 0; i <= array.length - 1; ++i) {
            if(  i === 0 ) {
                // console.log(i)
                tmpRes = new ListNode(array[i]);
            }
            else {
                // console.log(i)
                var tmptmp = tmpRes;
                tmpRes = new ListNode(array[i]);
                tmpRes.next = tmptmp;
            }
        }
        return tmpRes // Accepted
    };

Log in to reply
 

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