Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (55.6k points)

Can anyone tell me how to merge two sorted linked lists in Python?

1 Answer

0 votes
by (119k points)

Here is the recursive algorithm to merge two sorted linked lists in Python:

def merge_lists(h1, h2):

    if h1 is None:

        return h2

    if h2 is None:

        return h1

 

    if (h1.value < h2.value):

        h1.next = merge_lists(h1.next, h2)

        return h1

    else:

        h2.next = merge_lists(h2.next, h1)

        return h2

I recommend enrolling in this Python Course by Intelllipaat to learn Python.

Also, you can watch this video on the linked list in Python:

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 30, 2020 in Python by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Oct 30, 2020 in Python by Sudhir_1997 (55.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...