Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class SinglyLinkedList:
  2. #constructor
  3. def __init__(self):
  4. self.head = None
  5.  
  6. #method for setting the head of the Linked List
  7. def setHead(self,head):
  8. self.head = head
  9.  
  10. #method for deleting a node having a certain data
  11. #method for deleting a node having a certain data
  12. def delete(self,data):
  13. if self.head is None:
  14. return None
  15. else:
  16. cur = self.head
  17. prev = None
  18. while cur.data != data and cur.next is not None:
  19. prev = cur
  20. cur = cur.next
  21.  
  22. # when found
  23. if cur.data == data:
  24. # if head
  25. if cur == self.head:
  26. if cur.next is None:
  27. self.head = None
  28. else:
  29. self.head = cur.next
  30. else:
  31. if cur.next is None:
  32. prev.next = None
  33. else:
  34. prev.next = cur.next
  35. else:
  36. return None
Add Comment
Please, Sign In to add comment