Python Program For Writing A Function To Delete A Linked List Last Updated : 08 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Algorithm For Python:In Python, automatic garbage collection happens, so deleting a linked list is easy. Just need to change head to null. Implementation: Python3 # Python3 program to delete all # the nodes of singly linked list # Node class class Node: # Function to initialize the # node object def __init__(self, data): # Assign data self.data = data # Initialize next as null self.next = None # Constructor to initialize the # node object class LinkedList: # Function to initialize head def __init__(self): self.head = None def deleteList(self): # Initialize the current node current = self.head while current: # Move next node prev = current.next # Delete the current node del current.data # Set current equals prev node current = prev # In python garbage collection happens # therefore, only self.head = None # would also delete the link list # Push function to add node in # front of llist def push(self, new_data): # Allocate the Node & # Put in the data new_node = Node(new_data) # Make next of new Node as head new_node.next = self.head # Move the head to point to the # new Node self.head = new_node # Use push() to construct list # 1-> 12-> 1-> 4-> 1 if __name__ == '__main__': llist = LinkedList() llist.push(1) llist.push(4) llist.push(1) llist.push(12) llist.push(1) print("Deleting linked list") llist.deleteList() print("Linked list deleted") # This code is contributed by Shrikant13 Output: Deleting linked list Linked list deleted Time Complexity: O(n) Auxiliary Space: O(1) Please refer complete article on Write a function to delete a Linked List for more details! Comment More infoAdvertise with us Next Article Implementation of Queue using Linked List in Python K kartik Follow Improve Article Tags : Python Linked Lists Delete a Linked List Practice Tags : python Similar Reads Write a function to delete a Linked List Given a linked list, the task is to delete the linked list completely.Examples:Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput: NULLExplanation: Linked List is Deleted.Input: head: 1 -> 12 -> 1 -> 4 -> 1 -> NULLOutput: NULLExplanation: Linked List is Deleted.Table of C 9 min read Implementation of XOR Linked List in Python Prerequisite: XOR Linked List An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory-efficient version of Doubly Linked List can be created using only one space for the address field with every node. This memory efficient Doub 6 min read Python - Print list after removing element at given index In this article, we will explore different ways to remove an element from a list at a given index and print the updated list, offering multiple approaches for achieving this.Using pop()pop() method removes the element at a specified index and returns it.Pythonli = [10, 20, 30, 40, 50] index = 2 li.p 2 min read Implementation of Queue using Linked List in Python A queue is a linear data structure that follows the FIFO (First In First Out) principle. The first element added is the first one to be removed. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that ca 4 min read Javascript Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3.Method 4 min read Insert and delete at the beginning in Doubly Linked List in Python A doubly linked list is a collection of nodes. It is an advanced version of a singly linked list. In a doubly linked list, a node has three elements its data, a link to the next node, and a link to its previous node. In this article, we will learn how to insert a node at the beginning and delete a n 6 min read Like