• + 0 comments

    javascript:

    function mergeLists(head1, head2) {
        let newnode = new SinglyLinkedListNode(0, null)
        let tempnode = newnode
        
        while(head1 !== null && head2 !== null ){
            if(head1.data < head2.data){
                tempnode.next = head1
                head1 = head1.next //inc of head1 linked list
            }else{
                tempnode.next = head2
                head2 = head2.next  //inc of head2 linked list
            }
            tempnode = tempnode.next //inc of newnode
        }
        
        tempnode.next = head1 || head2 // if either head1 is null then tempnode will update with head2 or vice-versa
        
        return newnode.next
    }