Given a LinkedList object llist, the execution of the code
llist.linkedlist_to_plist()should return a built-in Python list with the same values, in the same order, as the linked list llist. If the linked list is empty, an empty Python list should be returned. The code for the LinkedList class is as follows:
class Node: def __init__(self, value): self._value = value self._next = None # getter for the _value attribute def value(self): return self._value # getter for the _next attribute def next(self): return self._next def __str__(self): return str(self._value) + "; " class LinkedList: def __init__(self): self._head = None # add a node to the head of the list def add(self, node): node._next = self._head self._head = node def __str__(self): string = 'List[ ' curr_node = self._head while curr_node != None: string += str(curr_node) curr_node = curr_node.next() string += ']' return string
Code:
ll = LinkedList()Result: [2,3,1]
ll.add(Node(1))
ll.add(Node(3))
ll.add(Node(2))
return ll.linkedlist_to_plist()
Code:
ll = LinkedList()Result:[20]
ll.add(Node(20))
return ll.linkedlist_to_plist()
Code:
ll = LinkedList()Result:[]
return ll.linkedlist_to_plist()
Code:
ll = LinkedList()Result:["cs120", "hello", "there"]
ll.add(Node("there"))
ll.add(Node("hello"))
ll.add(Node("cs120"))
return ll.linkedlist_to_plist()
Code:
ll = LinkedList()Result:["hello"]
ll.add(Node("hello"))
return ll.linkedlist_to_plist()