insert_at_index(self, index: int, value: object) -> None: This method inserts a new value at the specified index position in the linked list. Index 0 refers to the beginning of the list (right after the front sentinel). If the provided index is invalid, the method raises a custom "SLLException". Code for the exception is provided in the skeleton file. If the linked list contains N nodes (the sentinel node is not included in this count), valid indices for this method are [0, N] inclusive. Example #1: LinkedList () 1st test cases [(0, "A"), (0, "B"), (1, "c"), (3, "D"), (-1, "E"), (5, "F")) for index, value in test cases: print ("Inserted", value, "at index", index, ": ", end-"") try: 1st.insert_at_index (index, value) print (1st) except Exception as e: print (type (e)) Output: Inserted A at index 0: SLL (A) Inserted B at index 0: SLL (B -> A) SLL (B-> C -> A) Inserted C at index 1: Inserted D at index 3: SLL (B -> C -> A -> D) Inserted E at index -1 SLLException'>
class SLNode:
"""
Singly Linked List Node class
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
def __init__(self, value: object, next=None) -> None:
self.value = value
self.next = next
from SLNode import *
class SLLException(Exception):
"""
Custom exception class to be used by Singly Linked List
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
pass
class LinkedList:
def __init__(self, start_list=None) -> None:
"""
Initialize new linked list
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self._head = SLNode(None)
# populate SLL with initial values (if provided)
# before using this feature, implement insert_back() method
if start_list is not None:
for value in start_list:
self.insert_back(value)
def __str__(self) -> str:
"""
Return content of singly linked list in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = 'SLL ['
node = self._head.next
while node:
out += str(node.value)
if node.next:
out += ' -> '
node = node.next
out += ']'
return out
def length(self) -> int:
"""
Return the length of the linked list
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
length = 0
node = self._head.next
while node:
length += 1
node = node.next
return length
def is_empty(self) -> bool:
"""
Return True is list is empty, False otherwise
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return not self._head.next
def insert_at_index(self, index: int, value: object) -> None:
"""
this implementation
"""
pass
![insert_at_index(self, index: int, value: object) -> None:
This method inserts a new value at the specified index position in the linked list. Index 0
refers to the beginning of the list (right after the front sentinel).
If the provided index is invalid, the method raises a custom "SLLException". Code for the
exception is provided in the skeleton file. If the linked list contains N nodes (the sentinel
node is not included in this count), valid indices for this method are [0, N] inclusive.
Example #1:
1st LinkedList ()
test cases
[(0, "A"), (0, "B"), (1, "C"), (3, "D"), (-1, "E"), (5, "F")]
for index, value in test_cases:
print("Inserted", value, "at index", index, ": ", end-"")
try:
1st.insert_at_index (index, value)
print (1st)
except Exception as e:
print (type (e))
Output:
Inserted A at index 0: SLL [A]
Inserted B at index
Inserted C at
index 1:
SLL [B -> C -> A)
Inserted D at index 3: SLL [B -> C -> A -> D]
Inserted E at index -1 : <class ' main
Inserted F at index 5: <class main
0: SLL [B -> A)
SLLException'>
.SLLException'>](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F96eb680b-bf96-4b4b-a2da-be1777b93ea0%2Fbc544ec5-f53b-48be-8dc7-a25558b74771%2F52apjsp_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps









