We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Merge two sorted linked lists
  2. Discussions

Merge two sorted linked lists

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 168 Discussions, By:

recency

Please Login in order to post a comment

  • velinaivanova201
    2 days ago+ 0 comments

    Python

    def mergeLists(head1, head2): dummy_head = SinglyLinkedListNode(0) current = dummy_head

    while head1 is not None and head2 is not None:
        if head1.data < head2.data:
            current.next = head1
            head1 = head1.next
        else:
            current.next = head2
            head2 = head2.next
        current = current.next
    
    # If one of the lists is not empty, append the remaining nodes
    if head1 is not None:
        current.next = head1
    else:
        current.next = head2
    
    return dummy_head.next  # Return the head of the merged list
    
    0|
    Permalink
  • gerzsony
    4 days ago+ 0 comments

    PHP solution

    while($row = fgets(STDIN)){
        $rawnumbers[]= trim($row);
    }
    
    //get test cases no
    $t = $rawnumbers[0]; unset($rawnumbers[0]);
    
    for ($i=1; $i <= $t; $i++){
        if (count($rawnumbers) == 0) exit; //no more test case
        $rawnumbers = array_values($rawnumbers);
        
        //get positions af the two array delimiters (inside one test case)
        $first_group_len = $rawnumbers[0];  unset($rawnumbers[0]);
        $second_group_len_pos = $first_group_len + 1;  
        $second_group_len = $rawnumbers[$second_group_len_pos]; unset($rawnumbers[$second_group_len_pos]);
        
        //echo($first_group_len ."|".$second_group_len_pos ."|". $second_group_len."| ");
        
        //get the position of delimiter of the next test case
        $rawnumbers = array_values($rawnumbers);
        $splitposition = $first_group_len + $second_group_len;
        
        $outNumbers = array_slice($rawnumbers, 0, $splitposition );
        $rawnumbers = (array_slice($rawnumbers, $splitposition )); // next test case data if exists
        
        sort($outNumbers);
        echo implode(' ', $outNumbers). "\n";
    }
    
    0|
    Permalink
  • pierrealexis_no1
    1 week ago+ 0 comments

    JS Solution

    function mergeLists(head1, head2) {
     let list = new SinglyLinkedList();
      
     while(head1 || head2){
        let data1 = head1?.data || +Infinity
        let data2 = head2?.data || +Infinity    
        if(data1 < data2){
            list.insertNode(data1);
            head1 = head1?.next || null
        } else {
            list.insertNode(data2);
            head2 = head2?.next || null
        }
     }
        return list.head
    }
    
    0|
    Permalink
  • korostelevm
    2 weeks ago+ 0 comments

    JS

    function mergeLists(head1, head2) {
        
        let w = new SinglyLinkedListNode();
        let list = new SinglyLinkedList()
        let done = false;
        
        
        while (head1 || head2){
            
            if(!head1 && head2) {
                list.insertNode(head2.data)
                head2 = head2.next
                continue
            }
            if(!head2 && head1) {
                list.insertNode(head1.data)
                head1 = head1.next
                continue
            }
            
            if(head1.data < head2.data){
                list.insertNode(head1.data)
                head1 = head1.next
            }else{
                list.insertNode(head2.data)
                head2 = head2.next
            }
        }
        
        return list.head
    
    }
    
    0|
    Permalink
  • classmateada
    3 weeks ago+ 0 comments

    No template for C++20.

    C++:

    SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
        if (head1 == nullptr) {
            return head2;
        }
        if (head2 == nullptr) {
            return head1;
        }
        SinglyLinkedListNode* head = new SinglyLinkedListNode(0);
        SinglyLinkedListNode* p = head;
        while (head1 != nullptr && head2 != nullptr) {
            if (head1->data < head2->data) {
                p->next = head1;
                head1 = head1->next;
            } else {
                p->next = head2;
                head2 = head2->next;
            }
            p = p->next;
        }
        if (head1 == nullptr) {
            p->next = head2;
        } else if (head2 == nullptr) {
            p->next = head1;
        }
        return head->next;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy