| 88 | break |
| 89 | |
| 90 | def deleteNode(self, last, key): |
| 91 | |
| 92 | # If linked list is empty |
| 93 | if last == None: |
| 94 | return |
| 95 | |
| 96 | # If the list contains only a single node |
| 97 | if (last).data == key and (last).next == last: |
| 98 | |
| 99 | last = None |
| 100 | |
| 101 | temp = last |
| 102 | d = None |
| 103 | |
| 104 | # if last node is to be deleted |
| 105 | if (last).data == key: |
| 106 | |
| 107 | # find the node before the last node |
| 108 | while temp.next != last: |
| 109 | temp = temp.next |
| 110 | |
| 111 | # point temp node to the next of last i.e. first node |
| 112 | temp.next = (last).next |
| 113 | last = temp.next |
| 114 | |
| 115 | # travel to the node to be deleted |
| 116 | while temp.next != last and temp.next.data != key: |
| 117 | temp = temp.next |
| 118 | |
| 119 | # if node to be deleted was found |
| 120 | if temp.next.data == key: |
| 121 | d = temp.next |
| 122 | temp.next = d.next |
| 123 | |
| 124 | return last |
| 125 | |
| 126 | def traverse(self): |
| 127 | if self.last == None: |