Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def insert(self, value, index) -> None:
- curr = self._first
- cnt = 0
- if curr is None and index == 0:
- # when index is 0 and self._first is None
- self._first = Node(value)
- return None
- else:
- while curr is not None:
- # when reached at index
- if cnt == index:
- new_node = Node(curr.value)
- new_node._next = curr._next
- curr._value = value
- curr._next = new_node
- cnt += 1
- curr = curr._next
- return None
- raise IndexError
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement