0
0
DSA Pythonprogramming~10 mins

Merge Two Sorted Linked Lists in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the given value.

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = [1]
Drag options to blanks, or click blank then click option'
Aval
B0
CNone
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to 0 or val instead of None.
Confusing next with the node's value.
2fill in blank
medium

Complete the code to start merging by creating a dummy node.

DSA Python
def mergeTwoLists(l1, l2):
    dummy = Node([1])
    current = dummy
    # merging logic follows
Drag options to blanks, or click blank then click option'
A-1
BNone
Cl1.val
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using None as a value which causes errors.
Using l1.val which may not exist if l1 is None.
3fill in blank
hard

Fix the error in the while loop condition to merge until one list ends.

DSA Python
while l1 [1] None and l2 != None:
    if l1.val < l2.val:
        current.next = l1
        l1 = l1.next
    else:
        current.next = l2
        l2 = l2.next
    current = current.next
Drag options to blanks, or click blank then click option'
A==
B!=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which stops the loop immediately.
Using comparison operators like '<' or '>' which are invalid here.
4fill in blank
hard

Fill both blanks to attach the remaining nodes after the loop ends.

DSA Python
if l1 [1] None:
    current.next = l1
else:
    current.next = [2]
Drag options to blanks, or click blank then click option'
Ais not
Bis
Cl2
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'is' instead of 'is not' in the if condition.
Attaching None instead of the remaining list.
5fill in blank
hard

Fill the blanks to return the merged list skipping the dummy node.

DSA Python
def mergeTwoLists(l1, l2):
    dummy = Node(-1)
    current = dummy
    while l1 != None and l2 != None:
        if l1.val < l2.val:
            current.next = l1
            l1 = l1.next
        else:
            current.next = l2
            l2 = l2.next
        current = current.next
    if l1 is not None:
        current.next = l1
    else:
        current.next = l2
    return [1].[2]
Drag options to blanks, or click blank then click option'
Adummy
Bnext
Chead
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dummy instead of dummy.next.
Returning current or head which are not defined here.