Share my Javascript code,the runtime of which beats 95.83% of javascript submissions.


  • 0
    D
        /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} head
     * @return {ListNode}
     */
    var oddEvenList = function(head) {
        if(head === null || head.next === null || head.next.next === null){
            return head;
        }
        var secondHead = head.next, firstNode = head, secondNode = secondHead;
        while(firstNode !== null && secondNode !== null && secondNode.next !== null){
            firstNode.next = secondNode.next;
            firstNode = firstNode.next;
            secondNode.next = firstNode.next;
            secondNode = secondNode.next;
        }
        firstNode.next = secondHead;
        return head;
    };

Log in to reply
 

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