• + 2 comments

    Umm yeah i think i forgot that .... thanks for pointing it out :)

    • + 4 comments

      Thanks for sharing your solution Dark _ Knight19! I had figured out the "base case," but I was struggling with finishing the recursive part. I rewrote your code based on etayluz' comment and also slightly more efficient (it's less understandable than your code but less checks based on prior logic).

      if (headA == NULL && headB == NULL) return NULL;
      else if (headA == NULL) return headB;
      else if (headB == NULL) return headA;
      
      if(headA->data <= headB->data)
          headA->next = MergeLists(headA->next, headB);
      
      else {
          Node* temp = headB;
          headB = headB->next;
          temp->next = headA;
          headA = temp;
          headA->next = MergeLists(headA->next, headB);
      }
      
      return headA;
      
      • + 1 comment

        I think that the nested 'else if' wont be a much help in this case as the condition needs to be checked for all three 'if's in a case where A & B are not null. In case one of them is null it gets terminated without checking the remaining 'if's as it just simply returns a value. so I found no great optimization with nested 'else if' with this solution. Correct me if I am wrong. Happy coding.

        • + 1 comment

          You are right. We can just have:

          if(headA == NULL){
              return headB;
          } else if(headB == NULL){
              return headA;
          }
          
          • + 1 comment
            [deleted]
            • + 0 comments

              here is problem solution in python java c++ and c programming. https://solution.programmingoneonone.com/2020/07/hackerrank-merge-two-sorted-linked-lists-solution.html

      • + 5 comments

        for those who need it in java, here it is!

        Node mergeLists(Node headA, Node headB) {
            if (headA == null && headB == null) return null;
            else if (headA == null) return headB;
            else if (headB == null) return headA;
            
            if(headA.data <= headB.data)
                headA.next = mergeLists(headA.next, headB);
            else {
                Node temp = headB;
                headB = headB.next;
                temp.next = headA;
                headA = temp;
                headA.next = mergeLists(headA.next, headB);
            }
            return headA;
        }
        
        • + 1 comment

          Nice one. I did it this way Node mergeLists(Node headA, Node headB) { if (headA == null && headB == null) return null; else if (headA == null) return headB; else if (headB == null) return headA; if(headA.data <= headB.data){ headA.next = mergeLists(headA.next, headB); return headA; } else { headA.next = mergeLists(headA, headB.next); return headB; } }

          • + 1 comment

            did it work?

            • + 0 comments

              here is problem solution in python java c++ and c programming. https://solution.programmingoneonone.com/2020/07/hackerrank-merge-two-sorted-linked-lists-solution.html

        • + 0 comments

          Thanks

        • + 0 comments

          nice solution.

          The latter part of the code can be slightly shortened:

          if(headA.data >= headB.data) {
                  Node temp = headB;
                  headB = headB.next;
                  temp.next = headA;
                  headA = temp;
              }
              headA.next = mergeLists(headA.next,headB);
              return headA;
          
        • + 0 comments

          Code post mat kar hero. bakiyo ko bhi karne de thodi mehnat.

        • + 0 comments

          @pvamsii43 what is the difference if you write the last else condition as given below?

                      Node temp = headB;
              temp.next = headA;
              headA = temp;
                      headB = headB.next;
              headA.next = mergeLists(headA.next, headB);
          

          I tried this code and it gave a non terminating output.

      • + 0 comments

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

        }

      • + 0 comments

        sugoi

    • + 2 comments

      SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {

      SinglyLinkedListNode* temp;
      
      if(head1==NULL&&head2==NULL){
          return NULL;
      
      }
      
      if(head1==NULL) return head2;
      if(head2==NULL) return head1;
      
      if(head1->data<head2->data){
          head1->next=mergeLists(head1->next,head2);
          return head1;
      }
      
      else{
         head2->next=mergeLists(head2->next,head1);
          return head2;
      }
      

      } well..I think my code is easier to understand

      • + 1 comment

        where is mergeLists function

        • + 0 comments

          it is the main function thats we are calling again and again matlab recrusive

      • + 0 comments

        i don't think there is a need to check if both head1 and head2 are NULL.. question states that "either" may be NULL