Delete Alternate Nodes of a Linked List
“Delete Alternate Nodes of a Linked List” is a basic problem exercise for beginners based on linked list. Here, we are given a linked list and our task is to delete every alternate node of a linked list.
For example, we need to delete every alternate nodes of the following linked list: LINKED LIST: 11 -> 12 -> 13 -> 14 -> 15 -> 16 After deleting every other alternate nodes of a linked list: LINKED LIST: 11 -> 13 -> 15
The steps required to delete alternate nodes of a linked list are as follows:
- If the linked list is empty, then return.
- Traverse the complete linked list.
- While traversing, keep track of previous node of the node to be deleted and then delete the node by releasing the memory allocated for the node.
C++ Program to delete alternate nodes of a linked list is as follows:
/* C++ Program to delete alternate nodes of a Linked List */
#include<bits/stdc++.h>
using namespace std;
/* Structure of the node of the Linked List */
typedef struct node
{
int data;
struct node *next;
}Node;
/* Function to Insert node at the end of the linked list */
void insertEnd(Node **head,int ele)
{
/* Creating a new node */
Node *new_node = new Node();
new_node -> data = ele;
new_node -> next = NULL;
/* check whether the linked list is empty or not */
/* If Linked List is empty, then new node will become head of the linked list */
if(*head == NULL)
{
(*head) = new_node;
return;
}
/* If Linked List is not empty, then traverse the complete linked list and point the pointer
of last node of the linked list to the new node */
Node *temp = (*head);
while(temp->next != NULL)
temp = temp -> next;
temp -> next = new_node;
}
/* Function to Print the Linked List */
void print(Node *head)
{
Node *temp = head;
while(temp != NULL)
{
cout << temp -> data << " ";
temp = temp -> next;
}
cout<<endl;
}
/* Function to delete alternate nodes of the Linked List */
void deleteAlternate(Node *head)
{
/* If Linked List is empty, simply return */
if(head == NULL)
return;
Node *temp = head;
/* keep track of previous node of the node to be deleted and then
delete the node by releasing the memory allocated for the node. */
while(temp && temp->next)
{
Node *p = temp -> next;
temp -> next = temp -> next -> next;
temp = temp -> next;
free(p);
}
}
int main()
{
Node *head = NULL;
/* Inserting Some Nodes in the Linked List */
insertEnd(&head,11);
insertEnd(&head,12);
insertEnd(&head,13);
insertEnd(&head,14);
insertEnd(&head,15);
insertEnd(&head,16);
cout<<"Before Deleting Alternate Nodes of the Linked List:\n";
print(head);
deleteAlternate(head);
cout<<"\nAfter Deleting Alternate Nodes of the Linked List:\n";
print(head);
}
OUTPUT: Before Deleting Alternate Nodes of the Linked List: 11 12 13 14 15 16 After Deleting Alternate Nodes of the Linked List: 11 13 15
Related Posts:
- Delete Complete Linked List.
- Delete Nth Node of the Linked List.
- Delete without head pointer of the Linked List.
- Delete All Occurrences of particular node of the Linked List.
- Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
- Remove Duplicate Nodes from an Unsorted Linked List.
- Remove Duplicate Nodes from a Sorted Linked List.
- Find Union and Intersection of Two Linked List.
- Merge Two Sorted Linked List.
- Insert a New Node at the Sorted Linked List.
- Reverse a Linked List.
- Reverse a Linked List Using Stack.
- Printing Linked List in Reverse Order without actually reversing the Linked List.
- Swap Adjacent Elements of the Linked List.
- Count All Occurrences of a Particular Node in a Linked List.
- Bubble Sort on Linked List.
- Detect a Loop in a Linked List.
- Find the Length of the Loop present in the Linked List.
- Detect and Remove Loop from a Linked List.
- Segregate Even and Odd Nodes of the Linked List.