Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SinglyLinkedList:
- #constructor
- def __init__(self):
- self.head = None
- #method for setting the head of the Linked List
- def setHead(self,head):
- self.head = head
- #method for deleting a node having a certain data
- #method for deleting a node having a certain data
- def delete(self,data):
- if self.head is None:
- return None
- else:
- cur = self.head
- prev = None
- while cur.data != data and cur.next is not None:
- prev = cur
- cur = cur.next
- # when found
- if cur.data == data:
- # if head
- if cur == self.head:
- if cur.next is None:
- self.head = None
- else:
- self.head = cur.next
- else:
- if cur.next is None:
- prev.next = None
- else:
- prev.next = cur.next
- else:
- return None
Add Comment
Please, Sign In to add comment