C Program For Writing A Function To Delete A Linked List Last Updated : 21 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Algorithm For C:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C // C program to delete a linked list #include<stdio.h> #include<stdlib.h> #include<assert.h> // Link list node struct Node { int data; struct Node* next; }; // Function to delete the entire // linked list void deleteList(struct Node** head_ref) { // define head_ref to get the real head struct Node* current = *head_ref; struct Node* next; while (current != NULL) { next = current->next; free(current); current = next; } // Define head_ref to affect the real // head back in the caller. *head_ref = NULL; } /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(struct Node** head_ref, int new_data) { // Allocate node struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); // Put in the data new_node->data = new_data; // Link the old list of the new node new_node->next = (*head_ref); // Move the head to point to the new node (*head_ref) = new_node; } // Driver code int main() { // Start with the empty list struct Node* head = NULL; // Use push() to construct list // 1->12->1->4->1 push(&head, 1); push(&head, 4); push(&head, 1); push(&head, 12); push(&head, 1); printf("Deleting linked list"); deleteList(&head); printf("Linked list deleted"); } 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 Write a function to delete a Linked List K kartik Follow Improve Article Tags : C Language Linked Lists Delete a Linked List 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 Program to delete all even nodes from a Singly Linked List Given a singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input: LL = 1 -> 4 -> 3 -> 18 -> 19 Output: 1 -> 3 -> 19Input: LL = 5 -> 3 -> 6 -> 8 -> 4 -> 1 -> 2 -> 9 Output: 5 -> 3 -> 1 -> 9 Approach 15+ min read C Program to Implement Singly Linked List A linked list is a linear data structure used to store elements of the same data type but not in contiguous memory locations. It is a collection of nodes where each node contains a data field and a next pointer indicating the address of the next node. So only the current node in the list knows where 9 min read C Program for Reverse a linked list Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL I 4 min read Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the 8 min read Delete all the even nodes of a Circular Linked List Given a circular singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input : 57->11->2->56->12->61 Output : List after deletion : 57 -> 11 -> 61 Input : 9->11->32->6->13->20 Output : List after deletion : 9 - 10 min read Like