Sort by

recency

|

981 Discussions

|

  • + 0 comments

    My Java solution with linear time complexity and constant space complexity:

    static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
            //handle edge cases where there is no other list to merge
            if(head1 == null) return head2;
            if(head2 == null) return head1;
            
            //determine the new head of the merged linked list
            SinglyLinkedListNode head;
            if(head1.data <= head2.data){
                head = head1;
                head1 = head1.next;
            }
            else{
                head = head2;
                head2 = head2.next;
            }
            
            //iterate through both linked lists, sorting asc
            SinglyLinkedListNode curr = head;
            while(head1 != null && head2 != null){
                if(head1.data <= head2.data){
                    curr.next = head1;
                    head1 = head1.next;
                }
                else{
                    curr.next = head2;
                    head2 = head2.next;
                }
                curr = curr.next;
            }
            
            //append any extra vals to the merged linked list
            if(head1 != null) curr.next = head1;
            else if(head2 != null) curr.next = head2;
            
            return head;
        }
    
  • + 0 comments

    def mergeLists(head1, head2):

      new_list=SinglyLinkedList()
    
    current1=head1
    current2=head2
    
    while current1 and current2:
        if current1.data==current2.data:
            new_list.insert_node(current1.data)
            new_list.insert_node(current2.data)
            current1=current1.next
            current2=current2.next
        elif current1.data < current2.data:
            new_list.insert_node(current1.data)
            current1=current1.next
        elif current2.data < current1.data:
            new_list.insert_node(current2.data)
            current2=current2.next
    
    while current1:
        new_list.insert_node(current1.data)
        current1=current1.next
    
    while current2:
        new_list.insert_node(current2.data)
        current2=current2.next
    
    return new_list.head
    
  • + 0 comments
    result = SinglyLinkedList()
    while head1 or head2:
        if not head1:
            result.insert_node(head2.data)
            head2 = head2.next
        elif not head2:
            result.insert_node(head1.data)
            head1 = head1.next
        elif head1 and head2:
            if head1.data <= head2.data:
                result.insert_node(head1.data)
                head1 = head1.next
            else:
                result.insert_node(head2.data)
                head2 = head2.next
    return result.head
    
  • + 0 comments

    def mergeLists(head1, head2): dummy=SinglyLinkedListNode(0) cur=dummy while head1 and head2:

        if head1.data<=head2.data:
            cur.next=head1
            head1=head1.next
        else:
            cur.next=head2
            head2=head2.next
        cur=cur.next
    if head1:
        cur.next=head1
    if head2:
        cur.next=head2
    return dummy.next
    
  • + 0 comments

    function mergeLists(head1, head2) { let newNode = new SinglyLinkedListNode(); let tail = newNode;

    while(head1 !==null && head2 !== null){
        if(head1.data < head2.data){
            tail.next = head1;
            head1= head1.next;
        }else if(head2.data <= head1.data){
            tail.next = head2;
            head2 = head2.next;
        }
        tail = tail.next;
    
    
        if(head1 !==null){
            tail.next = head1;
        }else if(head2 !== null){
            tail.next = head2;
        }
    }
        return newNode.next;
    

    }