Java 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 Java:In Java, automatic garbage collection happens, so deleting a linked list is easy. Just need to change head to null. Implementation: Java // Java program to delete a linked list class LinkedList { // Head of the list Node head; // Linked List node class Node { int data; Node next; Node(int d) { data = d; next = null; } } // Function deletes the entire // linked list void deleteList() { head = null; } // Inserts a new Node at front // of the list. public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); // 3. Make next of new Node as head new_node.next = head; // 4. Move the head to point to new Node head = new_node; } public static void main(String [] args) { LinkedList llist = new LinkedList(); // Use push() to construct list // 1->12->1->4->1 llist.push(1); llist.push(4); llist.push(1); llist.push(12); llist.push(1); System.out.println("Deleting the list"); llist.deleteList(); System.out.println("Linked list deleted"); } } // This code is contributed by Rajat Mishra 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 Java Program For Writing A Function To Delete A Linked List K kartik Follow Improve Article Tags : Linked List Java Java Programs DSA Linked Lists Delete a Linked List +2 More Practice Tags : JavaLinked List Similar Reads Java 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 Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Java 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 Java 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- 8 min read Java 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 Like