Python Program For Writing A Function To Delete A Linked List Last Updated : 08 Dec, 2021 Comments Improve Suggest changes 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 Python Program For Writing A Function To Delete A Linked List kartik Follow Improve Article Tags : Linked List Python Python Programs DSA Linked Lists Delete a Linked List +2 More Practice Tags : Linked Listpython Similar Reads Python Program for Deleting a Node in a Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Python Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes 4 min read Python Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read Python Program For Removing Duplicates From A Sorted Linked List Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43- 7 min read Python Program to Implement Stack Using Linked List In Python, creating a stack using a linked list involves implementing a data structure where elements are added and removed in a last-in-first-out (LIFO) manner. This approach uses the concept of nodes interconnected by pointers, allowing efficient insertion and deletion operations. We are given a L 4 min read Python Program For Removing Duplicates From An Unsorted Linked List Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43. Recommended: 4 min read Python Program For Flattening A Linked List Given a linked list where every node represents a linked list and contains two pointers of its type: Pointer to next node in the main list (we call it 'right' pointer in the code below).Pointer to a linked list where this node is headed (we call it the 'down' pointer in the code below). All linked l 4 min read Python program to Search an Element in a Circular Linked List A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input: CList = 6->5->4->3->2 3 min read Python Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Python 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. Recomm 3 min read Like