0
0
DSA Pythonprogramming~10 mins

Why Circular Linked List and Real World Use Cases in DSA Python - Test Your Knowledge

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

Complete the code to create a circular linked list where the last node points back to the head.

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = [1]
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head
Drag options to blanks, or click blank then click option'
Aself.head
BNone
Cnew_node
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the last node's next to None breaks the circular structure.
Pointing the last node's next to itself creates a single-node loop only.
2fill in blank
medium

Complete the code to traverse and print all nodes in a circular linked list exactly once.

DSA Python
def print_list(cll):
    if not cll.head:
        return
    temp = cll.head
    while True:
        print(temp.data, end=' -> ')
        temp = [1]
        if temp == cll.head:
            break
    print('null')
Drag options to blanks, or click blank then click option'
Atemp
Bcll.head
Ctemp.prev
Dtemp.next
Attempts:
3 left
💡 Hint
Common Mistakes
Using temp instead of temp.next causes an infinite loop.
Using temp.prev is invalid for singly linked lists.
3fill in blank
hard

Fix the error in the code to correctly insert a new node after the head in a circular linked list.

DSA Python
def insert_after_head(cll, data):
    if not cll.head:
        cll.head = Node(data)
        cll.head.next = cll.head
    else:
        new_node = Node(data)
        new_node.next = cll.head.next
        cll.head.next = [1]
Drag options to blanks, or click blank then click option'
Acll.head
Bdata
Cnew_node
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head.next to data instead of the new node.
Assigning head.next to None breaks the list.
4fill in blank
hard

Fill both blanks to create a function that checks if a circular linked list contains a given value.

DSA Python
def contains(cll, value):
    if not cll.head:
        return False
    temp = cll.head
    while True:
        if temp.data == [1]:
            return [2]
        temp = temp.next
        if temp == cll.head:
            break
    return False
Drag options to blanks, or click blank then click option'
Avalue
BTrue
CFalse
Dcll.head
Attempts:
3 left
💡 Hint
Common Mistakes
Returning False when the value is found.
Comparing with cll.head instead of the value.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps each node's data to its next node's data in a circular linked list.

DSA Python
def map_to_next(cll):
    if not cll.head:
        return {}
    result = {}
    temp = cll.head
    while True:
        result[[1]] = [2]
        temp = [3]
        if temp == cll.head:
            break
    return result
Drag options to blanks, or click blank then click option'
Atemp.data
Btemp.next.data
Ctemp
Dtemp.next
Attempts:
3 left
💡 Hint
Common Mistakes
Using temp.next as key or value instead of temp.next.data.
Not updating temp inside the loop causing infinite loop.