Merge two sorted linked lists

  • + 0 comments

    C# solution after consulting geeksforgeeks when I got stuck

    static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
            
            if (head1 is null) return head2;
            if (head2 is null) return head1;
            
            if (head1.data <= head2.data)
            {
                head1.next = mergeLists(head1.next, head2);
                return head1;
            }
            else
            {
              head2.next = mergeLists(head1, head2.next);
              return head2; 
            }
        }