Print the alternate nodes of linked list (Iterative Method)
Last Updated :
20 Mar, 2023
Given a linked list, print the alternate nodes of the linked list.
Examples:
Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42
Output : 1 -> 3 -> 17 -> 29
Alternate nodes : 1 -> 3 -> 17 -> 29
Input : 10 -> 17 -> 33 -> 38 -> 73
Output : 10 -> 33 -> 73
Alternate nodes : 10 -> 33 -> 73
Approach :
- Traverse the whole linked list.
- Set count = 0.
- Print node when the count is even.
- Visit the next node.
Implementation:
C++
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void printAlternateNode( struct Node* head)
{
int count = 0;
while (head != NULL)
{
if (count % 2 == 0)
cout << head->data << " " ;
count++;
head = head->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;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
printAlternateNode(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printAlternateNode( struct Node* head)
{
int count = 0;
while (head != NULL) {
if (count % 2 == 0)
printf ( " %d " , head->data);
count++;
head = head->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;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
printAlternateNode(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static void printAlternateNode( Node head)
{
int count = 0 ;
while (head != null )
{
if (count % 2 == 0 )
System.out.printf( " %d " , head.data);
count++;
head = head.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;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 12 );
head = push(head, 29 );
head = push(head, 11 );
head = push(head, 23 );
head = push(head, 8 );
printAlternateNode(head);
}
}
|
Python3
class Node :
def __init__( self , data = None ) :
self .data = data
self . next = None
def push( self , data) :
new = Node(data)
new. next = self
return new
def printAlternateNode( self ) :
head = self
while head and head. next ! = None :
print (head.data, end = " " )
head = head. next . next
node = Node()
node = node.push( 12 )
node = node.push( 29 )
node = node.push( 11 )
node = node.push( 23 )
node = node.push( 8 )
node.printAlternateNode()
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static void printAlternateNode( Node head)
{
int count = 0;
while (head != null )
{
if (count % 2 == 0)
Console.Write( " {0} " , head.data);
count++;
head = head.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;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
printAlternateNode(head);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .next = null ;
}
}
function printAlternateNode(head){
let count = 0;
while (head != null )
{
if (count % 2 == 0)
console.log(head.data + " " );
count++;
head = head.next;
}
}
function push(head_ref, new_data){
let new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
let head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
printAlternateNode(head);
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Asked in : Govivace
Efficient Approach(Without using extra variable count):
Follow the below steps to solve the given problem:
1) We simply traverse the linked list but rather moving to the next node we will move next to next node at each time and print the head data.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* next;
Node( int data){
this ->data = data;
this ->next = NULL;
}
};
void printAlternateNode(Node* head){
while (head != NULL){
cout<<head->data<< " " ;
if (head->next != NULL){
head = head->next->next;
}
else {
head = head->next;
}
}
}
int main(){
Node* head = new Node(8);
head->next = new Node(23);
head->next->next = new Node(11);
head->next->next->next = new Node(29);
head->next->next->next->next = new Node(12);
printAlternateNode(head);
return 0;
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def printAlternateNode(head):
while (head is not None ):
print (head.data, end = " " )
if (head. next is not None ):
head = head. next . next
else :
head = head. next
head = Node( 8 )
head. next = Node( 23 )
head. next . next = Node( 11 )
head. next . next . next = Node( 29 )
head. next . next . next . next = Node( 12 )
printAlternateNode(head)
|
Java
public class LinkedList {
Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
public void printAlternateNode(Node head)
{
while (head != null ) {
System.out.print(head.data + " " );
if (head.next != null ) {
head = head.next.next;
}
else {
head = head.next;
}
}
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.head = new Node( 8 );
Node second = new Node( 23 );
Node third = new Node( 11 );
Node fourth = new Node( 29 );
Node fifth = new Node( 12 );
llist.head.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
llist.printAlternateNode(llist.head);
}
}
|
C#
using System;
public class Node {
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class Program {
public static void printAlternateNode(Node head)
{
while (head != null ) {
Console.Write(head.data + " " );
if (head.next != null ) {
head = head.next.next;
}
else {
head = head.next;
}
}
}
public static void Main()
{
Node head = new Node(8);
head.next = new Node(23);
head.next.next = new Node(11);
head.next.next.next = new Node(29);
head.next.next.next.next = new Node(12);
printAlternateNode(head);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .next = null ;
}
}
function printAlternateNode(head){
while (head != null ){
console.log(head.data + " " );
if (head.next != null ){
head = head.next.next;
}
else {
head = head.next;
}
}
}
let head = new Node(8);
head.next = new Node(23);
head.next.next = new Node(11);
head.next.next.next = new Node(29);
head.next.next.next.next = new Node(12);
printAlternateNode(head);
|
Time Complexity: O(N) where N is the number of nodes in the Linked List.
Auxiliary Space: O(1)