Complete the code to define a Node class with a constructor that initializes the data and next pointer.
class Node: def __init__(self, data): self.data = data self.[1] = None
The next attribute points to the next node in the list. It should be initialized to None when a new node is created.
Complete the code to initialize the head of the linked list to None in the LinkedList class constructor.
class LinkedList: def __init__(self): self.[1] = None
The head attribute points to the first node in the linked list. It should start as None when the list is empty.
Fix the error in the append method to correctly add a new node at the end of the linked list.
def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: last = self.head while last.[1] is not None: last = last.next last.next = new_node
The loop should check the next pointer to find the last node. Using next correctly traverses the list.
Fill both blanks to create a method that prints all node data in the linked list.
def print_list(self): current = self.head while current is not None: print(current.[1]) current = current.[2]
To print the data, access the data attribute of the current node. To move forward, update current to current.next.
Fill all three blanks to create a method that counts the number of nodes in the linked list.
def length(self): count = 0 current = self.[1] while current is not None: count [2]= 1 current = current.[3] return count
Start from the head. Increase count by 1 each time you visit a node using count += 1. Move to the next node with current = current.next.