Print alternate nodes of a linked list using recursion

17 Feb, 2022
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++
Java
Python3
C#
Javascript
// CPP code to print alternate nodes// of a linked list using recursion#include <bits/stdc++.h>using namespace std; // A linked list nodestruct Node {    int data;    struct Node* next;}; // Inserting node at the beginningvoid 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 codeint 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;}
// Java code to print alternate nodes// of a linked list using recursionclass GFG{ // A linked list nodestatic class Node{    int data;    Node next;}; // Inserting node at the beginningstatic 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 codepublic static void main(String args[]){    // Start with the empty list    Node head = null;     // construct 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
# Python3 code to print alternate nodes# of a linked list using recursion # A linked list nodeclass Node:    def __init__(self, data):        self.data = data        self.next = None # Inserting node at the beginningdef push( head_ref, new_data):     new_node = Node(new_data);    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.def printAlternate( node, isOdd):    if (node == None):        return;    if (isOdd == True):        print( node.data, end = " ");    if (isOdd == True):        isOdd = False;    else:        isOdd = True;    printAlternate(node.next, isOdd); # Driver code # Start with the empty listhead = None; # construct below list# 1->2->3->4->5->6->7->8->9->10head = 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 29AjayKumar
// C# code to print alternate nodes// of a linked list using recursionusing System; class GFG{  // A linked list nodepublic class Node{    public int data;    public Node next;};  // Inserting node at the beginningstatic 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 codepublic static void Main(String []args){    // Start with the empty list    Node head = null;      // construct 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
<script>// javascript code to print alternate nodes// of a linked list using recursion     // A linked list nodeclass Node {    constructor(val) {        this.data = val;        this.next = null;    }}      // Inserting node at the beginning    function push(head_ref , new_data) {var 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.    function printAlternate(node,  isOdd) {        if (node == null)            return;        if (isOdd == true)            document.write(node.data + " ");        printAlternate(node.next, !isOdd);    }     // Driver code             // Start with the empty listvar head = null;         // construct 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 contributed by umadevi9616</script>
1 3 5 7 9
 
Print alternate nodes of a linked list using recursion | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersPrint alternate nodes of a linked list using recursion | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:52•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=ksyUny54Avk" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
andrew1234
29AjayKumar
umadevi9616
ankita_saini
simmytarika5
Linked List
Recursion
Linked List
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.