0
0
DSA Pythonprogramming~10 mins

Find Middle Element of Linked List 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, data):
        self.data = [1]
        self.next = None
Drag options to blanks, or click blank then click option'
ANone
Bvalue
Cnode
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning None instead of the input data.
2fill in blank
medium

Complete the code to move the slow pointer one step forward in the linked list.

DSA Python
slow = slow[1]
Drag options to blanks, or click blank then click option'
A.prev
B.next
C.next.next
D.data
Attempts:
3 left
💡 Hint
Common Mistakes
Moving two steps instead of one.
Trying to access a non-existent 'prev' attribute.
3fill in blank
hard

Fix the error in the while loop condition to correctly check if fast and fast.next exist.

DSA Python
while fast is not None and fast[1] is not None:
    slow = slow.next
    fast = fast.next.next
Drag options to blanks, or click blank then click option'
A.next
B.next.next
C.data
D.prev
Attempts:
3 left
💡 Hint
Common Mistakes
Checking fast.next.next instead of fast.next.
Checking fast.prev which does not exist.
4fill in blank
hard

Fill both blanks to create a function that returns the middle node's data.

DSA Python
def find_middle(head):
    slow = head
    fast = head
    while fast is not None and fast[1] is not None:
        slow = slow[2]
        fast = fast.next.next
    return slow.data
Drag options to blanks, or click blank then click option'
A.next
B.prev
C.next.next
D.data
Attempts:
3 left
💡 Hint
Common Mistakes
Using .prev which does not exist.
Moving slow two steps instead of one.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps nodes to their data if data is even.

DSA Python
result = [1]: [2] for node in nodes if node.data [3] 0
Drag options to blanks, or click blank then click option'
Anode
Bnode.data
C% 2 ==
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '% 2 =='.
Swapping key and value in the dictionary.