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.
- Write a function to get Nth node in a Linked List
- Recursive function to delete k-th node from linked list
- Write a function that counts the number of times a given int occurs in a Linked List
- Delete all occurrences of a given key in a linked list
- Delete middle of linked list
- Delete a linked list using recursion
- Delete a Linked List node at a given position
- Delete alternate nodes of a Linked List
- Delete a node in a Doubly Linked List
- Delete last occurrence of an item from linked list
- Delete every Kth node from circular linked list
- Delete a given node in Linked List under given constraints
- Delete all occurrences of a given key in a doubly linked list
- Delete all the even nodes of a Circular Linked List
- Delete all the even nodes from a Doubly Linked List