Back to Browse

Merge two sorted linked lists [HackerRank] | LinkedList | Data Structure | Interview

3.2K views
Feb 21, 2020
15:59

Problem Statement: You’re given the pointer to the head nodes of two sorted linked lists. The data in both lists will be sorted in ascending order. Change the next pointers to obtain a single, merged linked list which also has data in ascending order. Either head pointer given may be null meaning that the corresponding list is empty. #HackerRank #DataStructure #Interview Sample Problem: https://www.hackerrank.com/challenges/merge-two-sorted-linked-lists/problem Code sample: def mergeLists(head1, head2): p1=head1 p2=head2 if p1.data is less than p2.data: // use angle bracket sign head=p1 t=p1 p1=p1.next else: head=p2 t=p2 p2=p2.next while(p1!=None and p2!=None): if p1.data is less than p2.data: // use angle bracket sign t.next=p1 p1=p1.next t=t.next else: t.next=p2 p2=p2.next t=t.next if p1==None: t.next=p2 else: t.next=p1 return head For 1 : 1 Tutoring WhatsApp contact : 7278222619 mail: [email protected] You can support via UPI : sattujaiswal@okhdfcbank Follow me on: Whatsapp: https://chat.whatsapp.com/LNwHGukUizjJPAxK7ma8gs Facebook: https://www.facebook.com/coderscart/ Linkedin: https://www.linkedin.com/in/satyendra-jaiswal-903588a2/ Instagram: https://www.instagram.com/codingcart/

Download

1 formats

Video Formats

360pmp420.8 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Merge two sorted linked lists [HackerRank] | LinkedList | Data Structure | Interview | NatokHD