Question: IN PYTHON You must complete one function: remove_middle remove_middle(data: DLL) -> DLL: - Removes the middle node(s) from a doubly linked list. If the list
IN PYTHON
You must complete one function: remove_middle
remove_middle(data: DLL) -> DLL: - Removes the middle node(s) from a doubly linked list. If the list is of odd length, this will be one node. If the list is of even length, this will be two. - param data: A doubly linked list, defined by the class provided - return: the modified DLL - MUST NOT use any extra containers, such as python lists or dictionaries. Use of an extra container will result in a 0 for the problem. - MUST NOT instantiate a new list, instead modify and return the existing list
from typing import TypeVar, List, Tuple
T = TypeVar("T") # represents generic type Node = TypeVar("Node") # represents a Node object (forward-declare to use in Node __init__)
class Node: """ Implementation of a doubly linked list node. Do not modify. """ __slots__ = ["value", "next", "prev"]
def __init__(self, value: T, next: Node = None, prev: Node = None) -> None: """ Construct a doubly linked list node. :param value: value held by the Node. :param next: reference to the next Node in the linked list. :param prev: reference to the previous Node in the linked list. :return: None. """ self.next = next self.prev = prev self.value = value
def __repr__(self) -> str: """ Represents the Node as a string. :return: string representation of the Node. """ return str(self.value)
def __str__(self) -> str: """ Represents the Node as a string. :return: string representation of the Node. """ return str(self.value)
class DLL: """ Implementation of a doubly linked list without padding nodes. Modify only below indicated line. """ __slots__ = ["head", "tail", "size"]
def __init__(self) -> None: """ Construct an empty doubly linked list. :return: None. """ self.head = self.tail = None self.size = 0
def __repr__(self) -> str: """ Represent the DLL as a string. :return: string representation of the DLL. """ result = "" node = self.head while node is not None: result += str(node) if node.next is not None: result += " <-> " node = node.next return result
def __str__(self) -> str: """ Represent the DLL as a string. :return: string representation of the DLL. """ return repr(self)
####### MODIFY BELOW ######
def remove_middle(data: DLL) -> DLL: """ Removes the middle node(s) from a doubly linked list :param data: a DLL :return: the modified DLL """
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
