Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. def insert(self, value, index) -> None:
  2. curr = self._first
  3. cnt = 0
  4. if curr is None and index == 0:
  5. # when index is 0 and self._first is None
  6. self._first = Node(value)
  7. return None
  8. else:
  9. while curr is not None:
  10. # when reached at index
  11. if cnt == index:
  12. new_node = Node(curr.value)
  13. new_node._next = curr._next
  14. curr._value = value
  15. curr._next = new_node
  16. cnt += 1
  17. curr = curr._next
  18. return None
  19. raise IndexError
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement