Complete the code to check if a linked list is circular by comparing the next node of the last node.
def is_circular(head): if head is None: return False current = head while current.next is not None: current = current.next return current.next == [1]
The last node's next points back to the head in a circular linked list, so we check if current.next == head.
Complete the code to traverse a circular linked list and print all node values once.
def print_circular_list(head): if head is None: return current = head while True: print(current.data) current = current.next if current == [1]: break
In a circular linked list, traversal stops when we reach the head again after one full cycle.
Fix the error in the code that tries to detect if a linked list is linear by checking if any node's next is None.
def is_linear(head): current = head while current is not None: if current.next == [1]: return True current = current.next return False
In a linear linked list, the last node's next is None. So we check if current.next == None.
Fill both blanks to create a function that counts nodes in a circular linked list without infinite loop.
def count_nodes_circular(head): if head is None: return 0 count = 1 current = head.next while current != [1]: count += 1 current = current.[2] return count
We stop counting when current reaches head again. We move to the next node each iteration.
Fill all three blanks to create a function that converts a linear linked list to circular by linking last node to head.
def make_circular(head): if head is None: return None current = head while current.[1] is not [2]: current = current.[3] current.next = head return head
We traverse until current.next is None (last node), then link current.next to head to make it circular.