Deletion

Removing an Item form a Linked List

We can remove an existing node using the key for that node. In the below program we locate the previous node of the node which is to be deleted. Then point the next pointer of this node to the next node of the node to be deleted.

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

 
class SLinkedList:
    def __init__(self):
        self.head = None

 
    def Atbegining(self, data_in):
        NewNode = Node(data_in)
        NewNode.next = self.head
        self.head = NewNode
                                     
# Function to remove node
    def RemoveNode(self, Removekey):

 
        HeadVal = self.head

 
        if (HeadVal is not None):
            if (HeadVal.data == Removekey):
                self.head = HeadVal.next
                HeadVal = None
                return

 
        while (HeadVal is not None):
            if HeadVal.data == Removekey:
                break
            prev = HeadVal
            HeadVal = HeadVal.next

 
        if (HeadVal == None):
            return

 
        prev.next = HeadVal.next

 
        HeadVal = None

 
    def LListprint(self):
        printval = self.head
        while (printval):
            print(printval.data),
            printval = printval.next

 
llist = SLinkedList()
llist.Atbegining("Mon")
llist.Atbegining("Tue")
llist.Atbegining("Wed")
llist.Atbegining("Thu")
llist.RemoveNode("Tue")
llist.LListprint()

When the above code is executed, it produces the following result:

Thu
Wed
Mon

 

Try here>>>