Skip to content

Delete function in LinkedList.py not working #52

Open
@bharathikannann

Description

@bharathikannann

@OmkarPathak, In the delete method in linkedlist.py the last element will be deleted if the given key is not present and if only the head node is present then there will a reference error for prev node.

So I have made some changes and it works

# deleting an item based on data(or key)
def delete(self, data):
    temp = self.head
    # if data/key is found in head node itself
    if(temp.data == data):
            self.head = temp.next
            temp = None
            return
        else:
            #  else search all the nodes
            while(temp.next):
                if(temp.data == data):
                    break
                prev = temp          #save current node as previous so that we can go on to next node
                temp = temp.next
            
            # node not found
            if temp == None:
                return
            
            prev.next = temp.next
            return

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions