0
0
DSA Pythonprogramming~10 mins

Sort a Linked List Using Merge Sort 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 find the middle node of the linked list.

DSA Python
def get_middle(head):
    if head is None:
        return head
    slow = head
    fast = head.next
    while fast and fast.next:
        slow = slow.[1]
        fast = fast.next.next
    return slow
Drag options to blanks, or click blank then click option'
Arandom
Bprev
Cchild
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors because singly linked lists don't have 'prev'.
Using 'fast.next' instead of 'fast.next.next' for the fast pointer.
2fill in blank
medium

Complete the code to merge two sorted linked lists.

DSA Python
def merge_sorted_lists(l1, l2):
    dummy = ListNode(0)
    tail = dummy
    while l1 and l2:
        if l1.val < l2.val:
            tail.next = l1
            l1 = l1.[1]
        else:
            tail.next = l2
            l2 = l2.next
        tail = tail.next
    tail.next = l1 or l2
    return dummy.next
Drag options to blanks, or click blank then click option'
Anext
Bprev
Cchild
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked lists.
Using 'child' or 'random' which are not standard pointers.
3fill in blank
hard

Fix the error in the merge_sort function to correctly split the list.

DSA Python
def merge_sort(head):
    if not head or not head.next:
        return head
    middle = get_middle(head)
    next_to_middle = middle.next
    middle.[1] = None
    left = merge_sort(head)
    right = merge_sort(next_to_middle)
    sorted_list = merge_sorted_lists(left, right)
    return sorted_list
Drag options to blanks, or click blank then click option'
Anext
Bprev
Cchild
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Setting middle.prev which does not exist in singly linked lists.
Not breaking the list, causing infinite recursion.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

DSA Python
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using 'word' instead of 'len(word)' for dictionary values.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.

DSA Python
data = {'a': 1, 'b': 0, 'c': 3}
result = { [1] : [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upper()' for keys.
Using '<' instead of '>' for filtering values.