Print alternate nodes of a linked list using recursion

Given a linked list, print alternate nodes of this linked list.

Examples :

Input : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
Output : 1 -> 3 -> 5 -> 7 -> 9 

Input : 10 -> 9
Output : 10

Recursive Approach :
1. Initialize a static variable(say flag)
2. If flag is odd print the node
3. increase head and flag by 1, and recurse for next nodes.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to print alternate nodes
// of a linked list using recursion
#include <bits/stdc++.h>
using namespace std;
  
// A linked list node
struct Node {
    int data;
    struct Node* next;
};
  
// Inserting node at the beginning
void push(struct Node** head_ref, int new_data)
    struct Node* new_node = 
       (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
  
// Function to print alternate nodes of linked list.
// The boolean flag isOdd is used to find if the current
// node is even or odd.
void printAlternate(struct Node* node, bool isOdd=true)
{
    if (node == NULL)
       return;
    if (isOdd == true)
        cout << node->data << " "
    printAlternate(node->next, !isOdd);
}
  
// Driver code
int main()
{
    // Start with the empty list
    struct Node* head = NULL;
  
    // construct below list
    // 1->2->3->4->5->6->7->8->9->10
  
    push(&head, 10);
    push(&head, 9);
    push(&head, 8);
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
  
    printAlternate(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to print alternate nodes 
// of a linked list using recursion 
class GFG 
{
  
// A linked list node 
static class Node 
    int data; 
    Node next; 
}; 
  
// Inserting node at the beginning 
static Node push( Node head_ref, int new_data) 
    Node new_node = new Node(); 
    new_node.data = new_data; 
    new_node.next = (head_ref); 
    (head_ref) = new_node; 
    return head_ref;
  
// Function to print alternate nodes of linked list. 
// The boolean flag isOdd is used to find if the current 
// node is even or odd. 
static void printAlternate( Node node, boolean isOdd) 
    if (node == null
    return
    if (isOdd == true
        System.out.print( node.data + " "); 
    printAlternate(node.next, !isOdd); 
  
// Driver code 
public static void main(String args[])
    // Start with the empty list 
    Node head = null
  
    // con below list 
    // 1.2.3.4.5.6.7.8.9.10 
  
    head = push(head, 10); 
    head = push(head, 9); 
    head = push(head, 8); 
    head = push(head, 7); 
    head = push(head, 6); 
    head = push(head, 5); 
    head = push(head, 4); 
    head = push(head, 3); 
    head = push(head, 2); 
    head = push(head, 1); 
  
    printAlternate(head,true); 
  
}
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to print alternate nodes 
// of a linked list using recursion 
using System;
  
class GFG 
{
   
// A linked list node 
public class Node 
    public int data; 
    public Node next; 
}; 
   
// Inserting node at the beginning 
static Node push( Node head_ref, int new_data) 
    Node new_node = new Node(); 
    new_node.data = new_data; 
    new_node.next = (head_ref); 
    (head_ref) = new_node; 
    return head_ref;
   
// Function to print alternate nodes of linked list. 
// The boolean flag isOdd is used to find if the current 
// node is even or odd. 
static void printAlternate( Node node, bool isOdd) 
    if (node == null
    return
    if (isOdd == true
        Console.Write( node.data + " "); 
    printAlternate(node.next, !isOdd); 
   
// Driver code 
public static void Main(String []args)
    // Start with the empty list 
    Node head = null
   
    // con below list 
    // 1.2.3.4.5.6.7.8.9.10 
   
    head = push(head, 10); 
    head = push(head, 9); 
    head = push(head, 8); 
    head = push(head, 7); 
    head = push(head, 6); 
    head = push(head, 5); 
    head = push(head, 4); 
    head = push(head, 3); 
    head = push(head, 2); 
    head = push(head, 1); 
   
    printAlternate(head,true); 
   
}
}
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

1 3 5 7 9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234, 29AjayKumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.