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:

  1. If the linked list is empty, then return.
  2. Traverse the complete linked list.
  3. 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:

  1. Delete Complete Linked List.
  2. Delete Nth Node of the Linked List.
  3. Delete without head pointer of the Linked List.
  4. Delete All Occurrences of particular node of the Linked List.
  5. Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
  6. Remove Duplicate Nodes from an Unsorted Linked List.
  7. Remove Duplicate Nodes from a Sorted Linked List.
  8. Find Union and Intersection of Two Linked List.
  9. Merge Two Sorted Linked List.
  10. Insert a New Node at the Sorted Linked List.
  11. Reverse a Linked List.
  12. Reverse a Linked List Using Stack.
  13. Printing Linked List in Reverse Order without actually reversing the Linked List.
  14. Swap Adjacent Elements of the Linked List.
  15. Count All Occurrences of a Particular Node in a Linked List.
  16. Bubble Sort on Linked List.
  17. Detect a Loop in a Linked List.
  18. Find the Length of the Loop present in the Linked List.
  19. Detect and Remove Loop from a Linked List.
  20. Segregate Even and Odd Nodes of the Linked List.

You may also like...