Intellipaat Back

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

In python,Can anyone tell me how to merge two sorted linked list into one Linked list?

Look at the below code:

def merge_lists(head1, head2):

    if head1 is None and head2 is None:

        return None

    if head1 is None:

        return head2

    if head2 is None:

        return head1

    if head1.value < head2.value:

        temp = head1

    else:

        temp = head2

    while head1 != None and head2 != None:

        if head1.value < head2.value:

            temp.next = head1

            head1 = head1.next

        else:

            temp.next = head2

            head2 = head2.next

    if head1 is None:

        temp.next = head2

    else:

        temp.next = head1

    return temp

    pass

the issue here is stucked in the endless loop.can anyone mention to me what the issue is

Examples are:

assert [] == merge_lists([],[])

 assert [1,2,3] == merge_lists([1,2,3], [])

 assert [1,2,3] == merge_lists([], [1,2,3])

 assert [1,1,2,2,3,3,4,5] == merge_lists([1,2,3], [1,2,3,4,5])

closed

4 Answers

0 votes
by (19k points)
 
Best answer
Given below is the code for Merging two sorted linked lists into one linked list in python:

def merge_lists(head1, head2):

    if head1 is None and head2 is None:

        return None

    if head1 is None:

        return head2

    if head2 is None:

        return head1

    merged_head = head1 if head1.value < head2.value else head2

    temp = merged_head

    while head1 is not None and head2 is not None:

        if head1.value < head2.value:

            temp.next = head1

            head1 = head1.next

        else:

            temp.next = head2

            head2 = head2.next

        temp = temp.next

    temp.next = head1 if head1 is not None else head2

    return merged_head
0 votes
by (26.4k points)

The issue with the current code is that it causes a result of the temp node's next before it explores the following node from the current hub. This is dangerous when the current temp node is the current node.

Imagine this case,

temp = N

temp.next = N  # which means N.next = N

N = N.next     # but from above N = (N.next = N) -> N = N

Check the below code for corrected version.

def merge_lists(head1, head2):

    if head1 is None:

        return head2

    if head2 is None:

        return head1

    # create dummy node to avoid additional checks in loop

    s = t = node() 

    while not (head1 is None or head2 is None):

        if head1.value < head2.value:

            # remember current low-node

            c = head1

            # follow ->next

            head1 = head1.next

        else:

            # remember current low-node

            c = head2

            # follow ->next

            head2 = head2.next

        # only mutate the node AFTER we have followed ->next

        t.next = c          

        # and make sure we also advance the temp

        t = t.next

    t.next = head1 or head2

    # return tail of dummy node

    return s.next

Interested to learn python in detail? Come and Join the python course.

0 votes
by (25.7k points)
The issue causing the endless loop in the provided code is that the temp variable is not being updated correctly within the while loop. This prevents the merged linked list from being constructed properly.

To fix the issue, you need to update the temp variable within each iteration of the loop to keep track of the current tail node of the merged list. Here's the corrected code:

def merge_lists(head1, head2):

    if head1 is None and head2 is None:

        return None

    if head1 is None:

        return head2

    if head2 is None:

        return head1

    if head1.value < head2.value:

        temp = head1

        head1 = head1.next

    else:

        temp = head2

        head2 = head2.next

    merged_head = temp  # Keep track of the merged list head

    while head1 is not None and head2 is not None:

        if head1.value < head2.value:

            temp.next = head1

            head1 = head1.next

        else:

            temp.next = head2

            head2 = head2.next

        temp = temp.next  # Update temp to the current tail node

    if head1 is None:

        temp.next = head2

    else:

        temp.next = head1

    return merged_head

Now the code correctly updates the temp variable within the loop, ensuring that the merged linked list is constructed properly.

The provided test cases should now work correctly with the corrected code.
0 votes
by (15.4k points)
Here's a  code along with an explanation of the issue and the fix:

def merge_lists(head1, head2):

    if head1 is None and head2 is None:

        return None

    if head1 is None:

        return head2

    if head2 is None:

        return head1

    if head1.value < head2.value:

        merged_head = head1

        head1 = head1.next

    else:

        merged_head = head2

        head2 = head2.next

    temp = merged_head  # Keep track of the current tail node of the merged list

    while head1 is not None and head2 is not None:

        if head1.value < head2.value:

            temp.next = head1

            head1 = head1.next

        else:

            temp.next = head2

            head2 = head2.next

        temp = temp.next  # Update temp to the current tail node

    if head1 is None:

        temp.next = head2

    else:

        temp.next = head1

    return merged_head

Explanation:

The provided code attempted to merge two sorted linked lists into a single linked list. However, it had an issue with an endless loop due to not correctly updating the temp variable within the while loop. This caused the merged linked list to be constructed incorrectly.

To fix the issue, the code has been modified to update the temp variable correctly within each iteration of the loop. This ensures that the current tail node of the merged list is maintained and that the merged linked list is constructed accurately.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...