I need to--> insert(self, val) Adds a node containing val to the list, keeping the list sorted in ascending order. In case of duplicates the new element is inserted either before or after the existing duplicates. If val is not an int, a ValueError is raised. I already implemented the class node and the class doubly linked list and here is my code for the insertion.
def insert(self, val: int) -> None:
"""Add a new node containing 'val' to the list, keeping the
list in ascending order.
Args:
val (int): Value to be added.
Raises:
ValueError: If val is not an int.
"""
# TODO
if isinstance(val,int):
new_node = node(val)
if self.size == 0:
self._head = new_node
self._tail = new_node
self.size += 1
return
else:#iterating through the list to get the proper location
current_node = self._head
while current_node is not None and current_node.val < val:
current_node = current_node.next_node
else:
raise ValueError
I iterated through the list to find the location,However i am stuck in the insertion.Any ideas? Am I on the right track?