Sort by

recency

|

977 Discussions

|

  • + 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;
    

    }

  • + 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
    }
    
  • + 0 comments

    Javascript solution:

    function mergeLists(head1, head2) {
      let head, list = getItems(head2).split(',')
      .concat(getItems(head1).split(','))
      .sort((a,b) => b-a)
    	
      for(let i =0; i<list.length; i++ ){
        let res = new SinglyLinkedListNode(list[i])
        res.next = head
        head = res
      }
       return head
    }
    
    function getItems(item){
      let list = []
      Object.entries(item).forEach((a,b) =>{
        if(typeof(a[1]) == 'object' && a[1] != null) list.push(getItems(a[1]))
        if(typeof(a[1]) == 'number') list.push(a[1])
      })
      return list.toString()
    }
    
  • + 0 comments

    Two ways

    def mergeLists(head1, head2):
        l, result = [], SinglyLinkedList()
        while head1:
            l.append(head1.data)
            head1 = head1.next
        while head2:
            l.append(head2.data)
            head2 = head2.next
        for data in sorted(l):
            result.insert_node(data)
        return result.head
    
    def mergeLists(head1, head2):
        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

    Solution in C++:

    SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
        
        SinglyLinkedListNode* head = NULL;
        if(head1 == NULL)
            return head2;
        else if(head2 == NULL)
            return head1;
        else
        {
            if(head1->data <= head2->data)
            {
                head = head1;
                head1 = head1->next;
            }
            else
            {
                head = head2;
                head2 = head2->next;
            }
            
            SinglyLinkedListNode* ptr;
            ptr = head;
            while(head1 != NULL || head2 != NULL)
            {
                if(head1 != NULL && head2 != NULL)
                {
                    if(head1->data <= head2->data)
                    {
                        ptr->next = head1;
                        head1 = head1->next;
                    }
                    else
                    {
                        ptr->next = head2;
                        head2 = head2->next;
                    }
                }
                else if(head1 != NULL)
                {
                    ptr->next = head1;
                    head1 = head1->next;
                }
                else if(head2 != NULL)
                {
                    ptr->next = head2;
                    head2 = head2->next;
                }
                ptr = ptr->next;
            }
        }        
        return head;
    }