Delete alternate nodes of a Linked List

Last Updated : 21 Apr, 2026

Given a Singly Linked List, starting from the second node delete all alternate nodes of it.

Examples:

Input: LinkedList: 1->2->3->4->5->6

420047217

Output: 1->3->5

420047216

Explanation: Deleting alternate nodes ie 2, 4, 6 results in the linked list with elements 1->3->5.

Try It Yourself
redirect icon

Input: LinkedList: 99->59->42->20

420047218

Output: 99->42

420047219

Recursive Approach - Time: O(n) Time O(n) Space

Traverse the linked list recursively and remove every alternate node. At each step, adjust the current node’s next pointer to bypass the next node, effectively deleting it. Then move to the next valid node using recursion and repeat the process until the end of the list is reached.

Algorithm:

  • If the node is NULL or node->next is NULL, stop recursion.
  • Store the next node to be deleted in a temporary pointer: temp = node->next
  • Update the link to skip the node: node->next = temp->next
  • Delete the skipped node: delete(temp)
  • Move to the next valid node using recursion: deleteAlt(node->next)

Note : temp and delete(temp) are needed in C and C++ only as automatic garbage collection does not happen there.

C++
#include <iostream>
using namespace std;

// A linked list node 
class Node 
{ 
public:
    int data; 
    Node *next; 
    Node(int val) {
        data = val;
        next = NULL;
    }
};

// deletes alternate nodes 
void deleteAlt(Node *head) 
{ 
    if (head == NULL) 
        return; 

    Node *prev = head; 
    Node *node = head->next; 

    while (prev!= NULL && node!= NULL) 
    { 
        prev->next = node->next; 
        delete node; 
      
        prev = prev->next; 
        if (prev!= NULL) 
            node = prev->next; 
    } 
} 

// print list
void printList(Node *node) 
{ 
    while (node!= NULL) 
    { 
        cout << node->data << " "; 
        node = node->next; 
    } 
} 

int main() 
{ 
    //linked list: 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 << "List before calling deleteAlt() \n"; 
    printList(head); 

    deleteAlt(head); 

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

    return 0; 
} 
C
#include <stdio.h>
#include <stdlib.h>

// A linked list node 
struct Node {
    int data;
    struct Node* next;
};

// Function to create a new node
struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}

// deletes alternate nodes 
void deleteAlt(struct Node* head) {
    if (head == NULL)
        return;

    struct Node* prev = head;
    struct Node* node = head->next;

    while (prev!= NULL && node!= NULL) {
        prev->next = node->next;
        free(node);
        prev = prev->next;
        if (prev!= NULL)
            node = prev->next;
    }
}

// print list
void printList(struct Node* node) {
    while (node!= NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}

int main() {
    //linked list: 1->2->3->4->5
    struct Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(5);

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

    deleteAlt(head);

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

    return 0;
}
Java
import java.io.*;

// A linked list node 
class Node {
    int data;
    Node next;
    Node(int val) {
        data = val;
        next = null;
    }
}

class LinkedList {
    
    // deletes alternate nodes 
    void deleteAlt(Node head) {
        if (head == null)
            return;

        Node prev = head;
        Node node = head.next;

        while (prev!= null && node!= null) {
            prev.next = node.next;
            node = null;
            prev = prev.next;
            if (prev!= null)
                node = prev.next;
        }
    }

    // print list
    void printList(Node node) {
        while (node!= null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }

    public static void main(String[] args) {
        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);

        LinkedList list = new LinkedList();
        System.out.println("List before calling deleteAlt()");
        list.printList(head);

        list.deleteAlt(head);

        System.out.println("\nList after calling deleteAlt()");
        list.printList(head);
    }
}
Python
# A linked list node
class Node:
    def __init__(self, val):
        self.data = val
        self.next = None

# deletes alternate nodes
def deleteAlt(head):
    if head is None:
        return

    prev = head
    node = head.next

    while prev is not None and node is not None:
        prev.next = node.next
        node = None
        prev = prev.next
        if prev is not None:
            node = prev.next

# print list
def printList(node):
    while node is not None:
        print(node.data, end=" ")
        node = node.next

# main
if __name__ == '__main__':
    
    # linked list: 1->2->3->4->5
    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(4)
    head.next.next.next.next = Node(5)

    print("List before calling deleteAlt()\n")
    printList(head)
    print()

    deleteAlt(head)

    print("List after calling deleteAlt()\n")
    printList(head)
    print()
C#
using System;

// A linked list node
public class Node
{
    public int data;
    public Node next;
    public Node(int val)
    {
        data = val;
        next = null;
    }
}

public class Program
{
    // deletes alternate nodes
    public static void deleteAlt(Node head)
    {
        if (head == null)
            return;

        Node prev = head;
        Node node = head.next;

        while (prev != null && node != null)
        {
            prev.next = node.next;   // unlink node

            prev = prev.next;

            if (prev != null)
                node = prev.next;
        }
    }

    // print list
    public static void printList(Node node)
    {
        while (node != null)
        {
            Console.Write(node.data + " ");
            node = node.next;
        }
    }

    public static void Main()
    {
        // linked list: 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);

        Console.WriteLine("List before calling deleteAlt()");
        printList(head);

        deleteAlt(head);

        Console.WriteLine("\nList after calling deleteAlt()");
        printList(head);
    }
}
JavaScript
class Node {
    constructor(val) {
        this.data = val;
        this.next = null;
    }
}

// deletes alternate nodes
function deleteAlt(head) {
    if (head === null) {
        return;
    }

    let prev = head;
    let node = head.next;

    while (prev!== null && node!== null) {
        prev.next = node.next;
        node = null;
        prev = prev.next;
        if (prev!== null) {
            node = prev.next;
        }
    }
}

// print list
function printList(node) {
    while (node!== null) {
        console.log(node.data + " ");
        node = node.next;
    }
}

// Main execution
let 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);

console.log("List before calling deleteAlt()");
printList(head);

deleteAlt(head);

console.log("\nList after calling deleteAlt()");
printList(head);

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

Iterative Approach - O(n) Time O(1) Space

Maintain a pointer to the node just before the one to be deleted, Update its next link to skip the unwanted node. Then move forward and repeat the process for the remaining list.

Algorithm:

Start from the head of the linked list.

  • If the list is empty (head == NULL), stop.
  • Set prev = head and node = head->next.
  • Traverse the list while both prev and node are not NULL.
  • Update the link to skip the node to be deleted: prev->next = node->next
  • Delete the node: delete(node)
  • Move prev to the next valid node: prev = prev->next
  • If prev is not NULL, update: node = prev->next

Note : delete(mode) ste needed in C and C++ only as automatic garbage collection does not happen there.

C++
#include <bits/stdc++.h>
using namespace std;

// A linked list node 
class Node 
{ 
public:
    int data; 
    Node *next; 
    Node(int val) {
        data = val;
        next = NULL;
    }
};

// deletes alternate nodes 
void deleteAlt(Node *head) 
{ 
    if (head == NULL) 
        return; 

    Node *prev = head; 
    Node *node = head->next; 

    while (prev != NULL && node != NULL) 
    { 
        prev->next = node->next; 
        delete(node);  

        prev = prev->next; 
        if (prev != NULL) 
            node = prev->next; 
    } 
} 

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

// Driver code 
int main() 
{ 
    // list: 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 << "List before deletion:\n"; 
    printList(head); 

    deleteAlt(head); 

    cout << "\nList after deleting:\n"; 
    printList(head); 

    return 0; 
}
C
#include <stdio.h>
#include <stdlib.h>

// A linked list node 
struct Node {
    int data; 
    struct Node *next; 
};

struct Node* newNode(int val) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = val;
    node->next = NULL;
    return node;
}

// deletes alternate nodes 
void deleteAlt(struct Node *head) {
    if (head == NULL) 
        return; 

    struct Node *prev = head; 
    struct Node *node = head->next; 

    while (prev!= NULL && node!= NULL) {
        prev->next = node->next; 
        free(node);
        prev = prev->next; 
        if (prev!= NULL) 
            node = prev->next; 
    }
}

// print linked list 
void printList(struct Node *node) {
    while (node!= NULL) {
        printf("%d ", node->data);
        node = node->next; 
    }
}

// Driver code 
int main() {
    // list: 1->2->3->4->5
    struct Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(5);

    printf("List before deletion:\n");
    printList(head);

    deleteAlt(head);

    printf("\nList after deleting:\n");
    printList(head);

    return 0;
}
Java
import java.util.*;

// A linked list node 
class Node {
    int data;
    Node next;
    Node(int val) {
        data = val;
        next = null;
    }
}

public class LinkedList {
    
    // deletes alternate nodes 
    static void deleteAlt(Node head) {
        if (head == null) 
            return;

        Node prev = head;
        Node node = head.next;

        while (prev!= null && node!= null) {
            prev.next = node.next;
            node = null; // for garbage collection
            prev = prev.next;
            if (prev!= null) 
                node = prev.next;
        }
    }

    // print linked list 
    static void printList(Node node) {
        while (node!= null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }

    // Driver code 
    public static void main(String[] args) {
        
        // list: 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);

        System.out.println("List before deletion:");
        printList(head);

        deleteAlt(head);

        System.out.println("\nList after deleting:");
        printList(head);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# deletes alternate nodes
def deleteAlt(head):
    if head is None:
        return

    prev = head
    node = head.next

    while prev is not None and node is not None:
        prev.next = node.next
        node = None
        prev = prev.next
        if prev is not None:
            node = prev.next

# print linked list
def printList(node):
    while node is not None:
        print(node.data, end=" ")
        node = node.next

# Driver code
if __name__ == '__main__':
    
    # list: 1->2->3->4->5
    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(4)
    head.next.next.next.next = Node(5)

    print("List before deletion:\n")
    printList(head)

    deleteAlt(head)

    print("\nList after deleting:\n")
    printList(head)
C#
using System;

public class Node
{
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GfG
{
    // deletes alternate nodes
    public static void deleteAlt(Node head)
    {
        if (head == null)
            return;

        Node prev = head;
        Node node = head.next;

        while (prev != null && node != null)
        {
            prev.next = node.next;  // unlink

            prev = prev.next;
            if (prev != null)
                node = prev.next;
        }
    }

    // print linked list
    public static void printList(Node node)
    {
        while (node != null)
        {
            Console.Write(node.data + " ");
            node = node.next;
        }
    }

    // Driver code
    public static void Main()
    {
        // list: 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);

        Console.WriteLine("List before deletion:");
        printList(head);

        deleteAlt(head);

        Console.WriteLine("\nList after deleting:");
        printList(head);
    }
}
JavaScript
/* A linked list node */
function Node(data) {
    this.data = data;
    this.next = null;
}

/* deletes alternate nodes */
function deleteAlt(head) {
    if (head == null) {
        return;
    }

    let prev = head;
    let node = head.next;

    while (prev!= null && node!= null) {
        prev.next = node.next;
        node = null;  // delete(node);

        prev = prev.next;
        if (prev!= null) {
            node = prev.next;
        }
    }
}

/* print linked list */
function printList(node) {
    while (node!= null) {
        console.log(node.data + " ");
        node = node.next;
    }
}

/* Driver code */
(function main() {
    
    /* list: 1->2->3->4->5 */
    let 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);

    console.log("List before deletion:");
    printList(head);

    deleteAlt(head);

    console.log("\nList after deleting:");
    printList(head);
})();

Output
List before deletion:
1 2 3 4 5 
List after deleting:
1 3 5 

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

Comment