What will be the output of the following code?class Node: def __init__(self, data=None): self.data = data self.next = Noneclass LinkedList: def __init__(self): self.head = None def print_list(self): current_node = self.head while current_node: print(current_node.data) current_node = current_node.next def insert_at_beginning(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_nodellist = LinkedList()llist.insert_at_beginning(3)llist.insert_at_beginning(2)llist.insert_at_beginning(1)llist.print_list()Options1 2 31 3 22 3 13 2 1
Question
What will be the output of the following code?class Node: def init(self, data=None): self.data = data self.next = Noneclass LinkedList: def init(self): self.head = None def print_list(self): current_node = self.head while current_node: print(current_node.data) current_node = current_node.next def insert_at_beginning(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_nodellist = LinkedList()llist.insert_at_beginning(3)llist.insert_at_beginning(2)llist.insert_at_beginning(1)llist.print_list()Options1 2 31 3 22 3 13 2 1
Solution
The output of the code will be:
1 2 3
So, the correct option is "1 2 3". This is because the insert_at_beginning method in the LinkedList class inserts new nodes at the beginning of the list. So, the last inserted node (which is 1) will be printed first, followed by 2 and then 3.
Similar Questions
What is the output of the following Python code snippet?class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node def display(self): current = self.head while current: print(current.data, end=" ") current = current.nextll = LinkedList()ll.append(1)ll.append(2)ll.display()
What does the following Python code snippet accomplish?class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node
What does the following Python code snippet accomplish?class Node: def __init__(self, data): self.data = data self.next = Nonenode1 = Node(1)node2 = Node(2)node1.next = node2Creates two linked list nodes with data 1 and 2, respectivelyCreates a singly linked list with two nodes containing data 1 and 2, respectivelyCreates a doubly linked list with two nodes containing data 1 and 2, respectivelyRaises a syntax error
What does the following Python code snippet accomplish?def reverse_linked_list(head): prev = None current = head while current is not None: next_node = current.next current.next = prev prev = current current = next_node head = prev return head
what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.