Merge two sorted linked lists

Sort by

recency

|

225 Discussions

|

  • + 0 comments

    Not very clean, but non-recursive solution that uses given class in js:

    function mergeLists(head1, head2) {
        if(head1 === null) return head2;
        if(head2 === null) return head1;
        let returnNode;
        if(head1.data < head2.data){
            returnNode = new SinglyLinkedListNode(head1.data);
            head1 = head1.next;
        }else {
            returnNode = new SinglyLinkedListNode(head2.data);
            head2 = head2.next
        }
        let currentNode = new SinglyLinkedListNode();
        returnNode.next = currentNode;
        while(head1 != null && head2 != null){
            if(head1.data <= head2.data){
                currentNode.data = head1.data;
                head1 = head1.next;
                currentNode.next = new SinglyLinkedListNode();
                currentNode = currentNode.next;
            }else if(head1.data > head2.data){
                currentNode.data = head2.data;
                head2 = head2.next;
                currentNode.next = new SinglyLinkedListNode();
                currentNode = currentNode.next;
            }
        }
        if(head1 === null && head2 != null){
            currentNode.data = head2.data;
            currentNode.next = head2.next;
        };
        if(head2 === null && head1 != null){
            currentNode.data = head1.data;
            currentNode.next = head1.next;
        };
        
        return returnNode;
    }
    
  • + 0 comments

    I solved it in ts using arrays istead of linked lists, bc I didn´t wanted to declare classes.

    And also I think the challenge is poorly explained.

    function main(il: string[]) { const arr: number[] = il.map(Number); const len = Number(arr.splice(0, 1)[0]);

    for (let i = 0; i < len; i++) {
        const res: number[] = [];
    
        for (let j = 0; j <= 1; j++) {
            const newLen = arr[0];
            arr.splice(0, 1);
    
            res.push(...arr.splice(0, newLen));
        }
    
        console.log(res.sort((a, b) => a - b).join(" "));
    }
    

    }

  • + 0 comments

    this challenge is poorly written

  • + 0 comments
    def mergeLists(head1, head2):
        dummy = SinglyLinkedListNode(0)         # A dummy node to start the merged list
        tail = dummy            # Tail is where we add new nodes
    
        while head1 and head2:
            if head1.data <= head2.data:
                tail.next = head1
                head1 = head1.next
            else:
                tail.next = head2
                head2 = head2.next
            tail = tail.next    # Move tail forward
    
        # Attach the rest of the non-empty list
        if head1:
            tail.next = head1
        elif head2:
            tail.next = head2
    
        return dummy.next  
        
    
  • + 0 comments

    There're mistakes in a description at the end fo the problem. Explanation of the list to be merged for list 1 has wrong values.