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 :
- Initialize a static variable(say flag)
- If flag is odd print the node
- increase head and flag by 1, and recurse for next nodes.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
void printAlternate( struct Node* node, bool isOdd= true )
{
if (node == NULL)
return ;
if (isOdd == true )
cout << node->data << " " ;
printAlternate(node->next, !isOdd);
}
int main()
{
struct Node* head = NULL;
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
class GFG
{
static class Node
{
int data;
Node next;
};
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;
}
static void printAlternate( Node node, boolean isOdd)
{
if (node == null )
return ;
if (isOdd == true )
System.out.print( node.data + " " );
printAlternate(node.next, !isOdd);
}
public static void main(String args[])
{
Node head = null ;
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 );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def 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;
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);
head = None ;
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 );
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
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;
}
static void printAlternate( Node node, bool isOdd)
{
if (node == null )
return ;
if (isOdd == true )
Console.Write( node.data + " " );
printAlternate(node.next, !isOdd);
}
public static void Main(String []args)
{
Node head = null ;
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 );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
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 printAlternate(node, isOdd) {
if (node == null )
return ;
if (isOdd == true )
document.write(node.data + " " );
printAlternate(node.next, !isOdd);
}
var head = null ;
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 );
</script>
|
Time complexity: O(N) where N is no of nodes in linked list
Auxiliary space: O(1), If we consider recursive call stack then it would be O(n)
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...