How to delete alternate nodes in a linked list
A linked list is a common data structure made up of one or more than one nodes. Each node contains a value and a pointer to the next node forming a chain-like structure. These nodes are stored randomly in the system's memory, which improves its space complexity compared to the array.
Understanding the problem
We get a linked list in which we want to remove all the alternate nodes from a list that begins with the same head node. We need to remove all the alternate nodes from the given list. Let's visualize it in the illustration below:
Simple solution
It is an easy approach in which we keep a record of the node's previous deletions. We move forward from one node to the next by modifying the next link iteratively. Let's see its step-by-step implementation:
//fuction for deleting alternate nodesvoid deleteAlternate(node *head_ptr){if (head_ptr == NULL)return;//Initialize prev and current nodenode *current = head_ptr;node *previous = head_ptr->link;while (current != NULL && previous != NULL){current->link = previous->link; //updating address linkdelete(previous); // delete the nodecurrent = current->link; //updating current nodeif (current != NULL)previous = current->link;}}int main(){node* list1=new node();list1 = NULL;list1=add_node(list1,0);add_node(list1,2);add_node(list1,4);add_node(list1,6);add_node(list1,8);cout << "Linked List: ";print_data(list1);deleteAlternate(list1);cout<<"\nAfter deletion: ";print_data(list1);}
Explanation
Lines 1–5: We define the
deleteAlternatefunction in which we delete alternate nodes. First, we check if the list is empty, and returnNULL.Lines 7–13: We initialize the nodes—
prevandcurrentto keep track of nodes. After updating thelinkof thecurrentnode, we delete thepreviousnode.Lines 15–19: We update the value of the
currentnode for the next iteration.
Time complexity: The time complexity for this approach is
Space complexity: The space complexity for this approach is
Recursive solution
The approach above is simple but not efficient. We can also solve this problem efficiently by using recursion. Despite being short and simple, the recursive code requires
// function to alternate nodesvoid deleteAlternate(node *head_ptr){if (head_ptr == NULL)return;node *current_node = head_ptr->link; //pointer to next nodeif (current_node == NULL)return;head_ptr->link = current_node->link; //changing next nodedelete(current_node); // delete nodedeleteAlternate(head_ptr->link); //recursively calling for each node}int main(){node* list1=new node();list1 = NULL;list1=add_node(list1,0);add_node(list1,2);add_node(list1,4);add_node(list1,6);add_node(list1,8);cout << "Linked List: ";print_data(list1);deleteAlternate(list1);cout<<"\nAfter deletion: ";print_data(list1);}
Explanation
Lines 1–5: We define the
deleteAlternatefunction in which we delete alternate nodes. First, we check if the list is empty, and returnNULL.Lines 7–12: We initialize the
currentnode to keep track of nodes. After updating thelinkof thecurrentnode, we check if it isNULL.Lines 14–17: We delete the
currentnode for the next iteration. We calldeleteAlternaterecursively until we reach theNULLpointer.
Time complexity: The time complexity for this approach is
Space complexity: The space complexity for this approach is
Free Resources