Algorithm For C/C++: Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted.
In Java, automatic garbage collection happens, so deleting a linked list is easy. We just need to change head to null.
Deleting linked list Linked list deleted
Time Complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Write a function to get Nth node in a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Recursive function to delete k-th node from linked list
- How to write C functions that modify head pointer of a Linked List?
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Delete a node in a Doubly Linked List
- Delete alternate nodes of a Linked List
- Delete a given node in Linked List under given constraints
- Delete N nodes after M nodes of a linked list
- Delete middle of linked list
- Delete all occurrences of a given key in a linked list
- Delete a Linked List node at a given position
- Delete last occurrence of an item from linked list
- Delete a Doubly Linked List node at a given position
- Delete all occurrences of a given key in a doubly linked list
- Delete a linked list using recursion
- Delete all Prime Nodes from a Singly Linked List
- Delete multiple occurrences of key in Linked list using double pointer
- Delete all the nodes from the doubly linked list that are greater than a given value