Gadgets 4 Students Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
2.2k views
in Data Structures and Algorithms by Goeduhub's Expert (2.2k points)
edited by
  • Deletion in singly Linked List  
  • Deletion from nth position in singly linked list : 

Where can I do online courses From Word's Top Instructors?

UDEMY::  Attend All Udemy Courses in Just INR 450[Coupon]
Coursera:: Join For FREE

1 Answer

0 like 0 dislike
by Goeduhub's Expert (2.2k points)
edited by
 
Best answer

Delete a node of linked list at given position

If node to be deleted is root, simply delete it. To delete a middle node, we must have a pointer to the node previous to the node to be deleted.

So if position is not zero, we run a loop position -1 times and get the pointer to the previous node.

deletion of node in singly linked list

# Node class 

class Node:

    # Function to initialize the node object 

    def __init__(selfdata=Nonenext=None):

        self.data = data # Assign data

        self.next = next # Initialize next as null 

# Linked List class 

class LinkedList:

    # Function to initialize the LinkedList object 

    def __init__(self):

        self.head = None

    # print function prints contents of linked list starting from head

    def print(self):

        # if linked list is empty

        if self.head is None:

            print("Linked list is empty")

            return

        itr = self.head

        #taking empty string and appending values

        llstr = ''

        while itr:

            llstr += str(itr.data)+' --> '

            itr = itr.next

        print(llstr)

    # Function to insert a new node at the beginning

    def insert_at_begining(selfdata):

        node = Node(data, self.head)

        self.head = node

    # Function to get length of linkedList

    def get_length(self):

        count = 0

        itr = self.head

        while itr:

            count+=1

            itr = itr.next

        return count

    #function to Remove an element at a given index 

    def remove_at(selfindex):

        #validate that index, if index is not a valid,raise the exception

        if index<0 or index>=self.get_length():

            raise Exception("Invalid Index")

        #remove the element at the beginning of the linked list if index is equal to 0  

        if index==0:

            self.head = self.head.next

            return

        count = 0

        itr = self.head

        while itr:

            #find previous element of the node to be deleted 

            if count == index - 1:

                itr.next = itr.next.next

                break

            itr = itr.next

            count+=1

    

# Code execution starts here     

if __name__ == '__main__':

    ll = LinkedList()

    

    ll.insert_at_begining("Red")

    ll.insert_at_begining("Green")

    ll.insert_at_begining("Black")

    ll.insert_at_begining("Yellow")

    

    print ("Created Linked List: ")

    ll.print()

    print ("Print Linked List after deletion at given position: ")

    ll.remove_at(2)

    ll.print()

Output:

Created Linked List:

Yellow --> Black --> Green --> Red -->

Print Linked List after deletion at given position:  

Yellow --> Black --> Red -->  

3.3k questions

7.1k answers

395 comments

4.5k users

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...