- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete alternate nodes of a Linked List in C++
In this tutorial, we are going to learn how to delete all prime nodes from a singly linked list.
Let's see the steps to solve the problem.
Write struct with data and next pointer.
Write a function to insert the node into the singly linked list.
Initialize the singly linked list with dummy data.
Iterate over the singly linked list.
Delete alternate node by maintaining the previous node.
Write a function to delete the node. Consider the following three cases while deleting the node.
If the node is head node, then move the head to next node.
If the node is middle node, then link the next node to the previous node
If the node is end node, then remove the previous node link. Let's see the code.
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *next; }; void deleteAlternateNodes(Node *head) { if (head == NULL) return; Node *prev = head; Node *node = head->next; while (prev != NULL && node != NULL) { prev->next = node->next; free(node); prev = prev->next; if (prev != NULL) { node = prev->next; } } } void insertNode(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void printLinkedList(Node *node) { while (node != NULL) { cout << node->data << " -> "; node = node->next; } } int main() { Node* head = NULL; insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); insertNode(&head, 4); insertNode(&head, 5); insertNode(&head, 6); cout << "Linked List before deletion:" << endl; printLinkedList(head); deleteAlternateNodes(head); cout << "\nLinked List after deletion:" << endl; printLinkedList(head); return 0; }
Output
If you execute the above program, then you will get the following result.
Linked List before deletion: 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> Linked List after deletion: 6 -> 4 -> 2 ->
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Sum of the alternate nodes of linked list in C++
- Print alternate nodes of a linked list using recursion in C++
- Delete N nodes after M nodes of a linked list in C++?
- Product of the alternate nodes of linked list
- Reverse Alternate K Nodes in a Singly Linked List in C++
- Delete N nodes after M nodes of a linked list in C++ program
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Print the alternate nodes of linked list (Iterative Method) in C language
- Delete all Prime Nodes from a Doubly Linked List in C++
- Delete all Prime Nodes from a Singly Linked List in C++
- Delete all Non-Prime Nodes from a Singly Linked List in C++
- Delete all the even nodes from a Doubly Linked List in C++
- Alternate sorting of Linked list in C++
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- Program to delete n nodes after m nodes from a linked list in Python
