Delete a Linked List node at a given position in C
C Program to Delete a Node in a Singly Linked List at a specific position
We will be writing a Program in C to delete a node at a given position from a Singly Linked List. We will look at different approaches to Delete a specific node in Linked List in C. Let us check the possible method below –
Approach to Delete a specific node in linked list C
- Accept the Initial Linked List from the user
- Accept the node position that the user wants to delete
- Check if it’s a valid position the value should range between [1, size] of the linked list
- Traverse till the (pos-1)th node
- Change the next pointer of (pos-1)th node to (pos+1)th node
- Free the memory for pos’th node.
Program for Singly Linked List deletion at a specific position in C
Run
// Program for Singly Linked List deletion at a specific position in C #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; // current size of Linked List int size = 0; void insert (struct Node **head, int data) { struct Node *new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = data; new_node->next = *head; *head = new_node; size++; } void deletePosition (int pos, struct Node **head) { struct Node *temp = *head; struct Node *prevNode; if (pos < 1 || pos > size) { printf ("Invalid\n"); return; } // delete the 1st node if (pos == 1) { *head = (*head)->next; printf ("%d deleted\n", temp->data); free (temp); size--; return; } // traverse to the pos'th node while (--pos) { prevNode = temp; temp = temp->next; } // change prevNode node's next node to nth node's next node prevNode->next = temp->next; // delete this nth node printf ("%d deleted\n", temp->data); free (temp); size--; } void display (struct Node *node) { printf ("Linked List : "); // as linked list will end when Node is Null while (node != NULL) { printf ("%d ", node->data); node = node->next; } printf ("\n"); } int main () { struct Node *head = NULL; insert (&head, 50); insert (&head, 40); insert (&head, 30); insert (&head, 20); insert (&head, 10); display (head); // delete the 2nd node deletePosition (2, &head); // delete the 4th node deletePosition (4, &head); display (head); return 0; }
Output
Linked List : 10 20 30 40 50 20 deleted 50 deleted Linked List : 10 30 40
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Login/Signup to comment