Open In App

Delete alternate nodes of a Linked List

Last Updated : 29 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3.
 

C++
// C++ program to remove alternate 
// nodes of a linked list 
#include <bits/stdc++.h>
using namespace std;

/* A linked list node */
class Node 
{ 
    public:
    int data; 
    Node *next; 
}; 

/* deletes alternate nodes 
of a list starting with head */
void deleteAlt(Node *head) 
{ 
    if (head == NULL) 
        return; 

    /* Initialize prev and node to be deleted */
    Node *prev = head; 
    Node *node = head->next; 

    while (prev != NULL && node != NULL) 
    { 
        /* Change next link of previous node */
        prev->next = node->next; 
        delete(node); // delete the node
        /* Update prev and node */
        prev = prev->next; 
        if (prev != NULL) 
            node = prev->next; 
    } 
} 

/* UTILITY FUNCTIONS TO TEST fun1() and fun2() */
/* 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(Node** head_ref, int new_data) 
{ 
    /* allocate node */
    Node* new_node = new 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; 
} 

/* Function to print nodes in a given linked list */
void printList(Node *node) 
{ 
    while (node != NULL) 
    { 
        cout<< node->data<<" "; 
        node = node->next; 
    } 
} 

/* Driver code */
int main() 
{ 
    /* Start with the empty list */
    Node* head = NULL; 

    /* Using push() to construct below list 
    1->2->3->4->5 */
    push(&head, 5); 
    push(&head, 4); 
    push(&head, 3); 
    push(&head, 2); 
    push(&head, 1); 

    cout<<"List before calling deleteAlt() \n"; 
    printList(head); 

    deleteAlt(head); 

    cout<<"\nList after calling deleteAlt() \n"; 
    printList(head); 

    return 0; 
} 

// This code is contributed by rathbhupendra
C Java Python3 C# JavaScript

Output
List before calling deleteAlt() 
1 2 3 4 5 
List after calling deleteAlt() 
1 3 5 


Time Complexity: O(n) 

where n is the number of nodes in the given Linked List.

Auxiliary Space: O(1)

As constant extra space is used.


Method 2 (Recursive) 
Recursive code uses the same approach as method 1. The recursive code is simple and short but causes O(n) recursive function calls for a linked list of size n. 
 

C++
#include <iostream>

using namespace std;

// Define a structure for the linked list node
struct Node {
    int data;
    Node* next;
    
    Node(int value) : data(value), next(nullptr) {}
};

// Function to delete alternate nodes using recursion
void deleteAlt(Node* node) {
    if (node == nullptr || node->next == nullptr) {
        return;
    }
    
    Node* temp = node->next;
    node->next = temp->next;
    delete temp;
    
    deleteAlt(node->next);
}

// Function to print the linked list
void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data << " ";
        head = head->next;
    }
    cout << endl;
}

int main() {
    // Create the linked list with the given input: 1, 2, 3, 4, 5
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);
    
    cout << "Original linked list: ";
    printList(head);
    
    // Call the function to delete alternate nodes
    deleteAlt(head);
    
    cout << "Modified linked list after deleting alternate nodes: ";
    printList(head);
    
    // Free memory
    Node* current = head;
    while (current != nullptr) {
        Node* next = current->next;
        delete current;
        current = next;
    }

    return 0;
}
C Java Python3 C# JavaScript

Output
Original linked list: 1 2 3 4 5 
Modified linked list after deleting alternate nodes: 1 3 5 

Time Complexity: O(n)

Auxiliary Space: O(1)

As this is a tail recursive function no function call stack is required thus the extra space used is constant.
 


Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem.
 



Next Article

Similar Reads

three90RightbarBannerImg