• + 13 comments

    If you move the return statement to inside your conditional blocks, you can clean up your headA > headB code by just allowing it to return headB. Here's my solution in Java:

    Node MergeLists(Node a, Node b) {
        if (a == null) {
            return b;
        } else if (b == null) {
            return a;
        }
    
        if (a.data < b.data) {
            a.next = MergeLists(a.next, b);
            return a;
        } else {
            b.next = MergeLists(a, b.next);
            return b;
        }
    }
    

    You can also test for one node == null at a time as I did; if either is null it returns the other, and if both are null returning the second node still returns null which is equivalent to what if((headA==NULL)&&(headB==NULL)) does.

    • + 0 comments

      Nicely done!!

    • + 3 comments
      if (headA == null || headB == null) {	
          return (headA == null) ? headB: headA;		
      }	
      if (headA.data < headB.data) {	
          headA.next = MergeLists(headA.next, headB);			
          return headA;	
      }	
      headB.next = MergeLists(headB.next, headA);		
      return headB;
      

      Do you think this is a bit simpler? I know it tests twice.

      PS: How to attach code?

      • + 2 comments

        You need to press enter twice followed by a tab:

        public static void main(String [] args)
        

        And then enter for a new line in the code. The text appears brown when it's formatted as code.

        • + 0 comments

          If you want syntax highlighting, you can use the following syntax:

          ```java
          (code)
          ```
          

          Replace "java" with the language of your choice. python3, ruby, c++, etc.

      • + 0 comments

        didn't work

      • + 0 comments

        Recursive approach are the best. How did you come up with a recursive approach. I am always solving questions with iterative approaches.

    • + 2 comments

      Can you please explain your code?

      • + 1 comment

        consider head1: 4 ->5 ->6 and head2: 1 ->2 ->10 i am sure you might have understood if head1 is null then return head2, and vice versa now with.

        1. if(head1.data>=head2.data) { head2.next=mergeList(head1,head2.next); return head2; }

          • this part of code essentially means, the next element to head 1(element 4) would be the result of the comparison between 4 and next head2's element which is 2.
          • now the result of the comparison between 4 and 2 would be assigned next to 4.
          • so, if head1's element is the largest, that ll be the final result of final comparison in this part of the code. but it would fail in the case, where 4(head1) is compared to 10(head2), where it goes to the else part.

            1. else { head1.next = mergeLists(head1.next, head2); return head1; }
          • here head1's next element,i.e 5 is compared to current head2 i.e 10, again first part of code(comparison if clause) is executed, where it again fails coz 10>5. then 10 is compared to 6, and again 10>6. so head1 is returned, which is 6,with the head2 s elemnt i.e 10. and if you observe closely , this comparison was assigned to head1(6)'s next.

          • now this 10 will bubble up to the previous part where it becomes 6->10. now this link moves up and becomes 5->6->10, which moves up to be 4->5->6->10, again 2->4->5->6->10 and finally becomes 1->2->4->5->6->10.

        hope it helped understand recursion.

    • + 0 comments

      Sweet..nice solution!

    • + 0 comments

      wow so simple to understand

    • + 0 comments

      amazing

    • + 0 comments

      This is pretty!

    • + 0 comments

      What's the time complexity of your solution?

    • + 0 comments

      I like this solution. What is the time complexity over in this solution.

    • + 0 comments

      I converted this to Python but Tests 5 and 6 are failing is it beacuse of recursion stack full?

    • [deleted]
      + 1 comment
      [deleted]
    • + 0 comments

      This is a beautiful solution