Given a linked list, the task is to print the sum of the alternate nodes of the linked list.
Examples:
Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42
Output : 50
Alternate nodes : 1 -> 3 -> 17 -> 29
Input : 10 -> 17 -> 33 -> 38 -> 73
Output : 116
Alternate nodes : 10 -> 33 -> 73
Iterative Approach:
- Traverse the whole linked list.
- Set sum = 0 and count=0.
- Add the data of the node to sum when the count is even.
- Visit the next node.
Below is the implementation of this approach:
C++
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
int sumAlternateNode( struct Node* head)
{
int count = 0;
int sum = 0;
while (head != NULL)
{
if (count % 2 == 0)
sum += head->data;
count++;
head = head->next;
}
return sum;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new 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);
cout << sumAlternateNode(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int sumAlternateNode( struct Node* head)
{
int count = 0;
int sum = 0;
while (head != NULL) {
if (count % 2 == 0)
sum += head->data;
count++;
head = head->next;
}
return sum;
}
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);
printf ( " %d " , sumAlternateNode(head));
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static int sumAlternateNode( Node head)
{
int count = 0 ;
int sum = 0 ;
while (head != null )
{
if (count % 2 == 0 )
sum += head.data;
count++;
head = head.next;
}
return sum;
}
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 );
System.out.printf( " %d " , sumAlternateNode(head));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def sumAlternateNode(head):
count = 0
sum = 0
while (head ! = None ):
if (count % 2 = = 0 ):
sum = sum + head.data
count = count + 1
head = head. next
return sum
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
if __name__ = = '__main__' :
head = None
head = push(head, 12 )
head = push(head, 29 )
head = push(head, 11 )
head = push(head, 23 )
head = push(head, 8 )
print (sumAlternateNode(head))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static int sumAlternateNode( Node head)
{
int count = 0;
int sum = 0;
while (head != null )
{
if (count % 2 == 0)
sum += head.data;
count++;
head = head.next;
}
return sum;
}
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);
Console.Write( " {0} " , sumAlternateNode(head));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function sumAlternateNode(head) {
var count = 0;
var sum = 0;
while (head != null ) {
if (count % 2 == 0)
sum += head.data;
count++;
head = head.next;
}
return sum;
}
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;
}
var head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
document.write( sumAlternateNode(head));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- As we are traversing the list only once.
- Auxiliary Space: O(1)
- As constant extra space is used.
Recursive Approach:
- Initialize a static variable(say flag).
- If the flag is odd, add the node with the sum.
- Increase head and flag by 1, and recurse for next nodes.
Below is the implementation of this approach:
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 = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void productAlternateNodes( struct Node* node,
int & sum, bool isOdd = true )
{
if (node == NULL)
return ;
if (isOdd == true )
sum = sum + (node->data);
productAlternateNodes(node->next, sum, !isOdd);
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
int sum = 0;
productAlternateNodes(head, sum);
cout << sum;
return 0;
}
|
Java
class GFG
{
static Node head= null ;
static int sum;
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=head_ref;
}
static void productAlternateNodes(Node node, boolean isOdd)
{
if (node == null )
return ;
if (isOdd == true )
sum = sum + (node.data);
productAlternateNodes(node.next, !isOdd);
}
public static void main(String[] args)
{
push(head, 12 );
push(head, 29 );
push(head, 11 );
push(head, 23 );
push(head, 8 );
sum = 0 ;
productAlternateNodes(head, true );
System.out.println(sum);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = next
def push(head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
sum = 0 ;
def subtractAlternateNodes(node, isOdd ):
global sum
if (node = = None ):
return ;
if (isOdd = = True ):
if ( sum = = 0 ) :
sum = node.data;
else :
sum = sum + (node.data);
subtractAlternateNodes(node. next , not isOdd);
head = None ;
head = push(head, 12 );
head = push(head, 29 );
head = push(head, 11 );
head = push(head, 23 );
head = push(head, 8 );
sum = 0 ;
subtractAlternateNodes(head, True );
print ( sum );
|
C#
using System;
class GFG
{
static Node head = null ;
static int sum;
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=head_ref;
}
static void productAlternateNodes(Node node, bool isOdd)
{
if (node == null )
return ;
if (isOdd == true )
sum = sum + (node.data);
productAlternateNodes(node.next, !isOdd);
}
public static void Main(String[] args)
{
push(head, 12);
push(head, 29);
push(head, 11);
push(head, 23);
push(head, 8);
sum = 0;
productAlternateNodes(head, true );
Console.WriteLine(sum);
}
}
|
Javascript
<script>
var head = null ;
var sum;
class Node {
constructor() {
this .data = 0;
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 = head_ref);
}
function productAlternateNodes(node, isOdd) {
if (node == null ) return ;
if (isOdd == true ) sum = sum + node.data;
productAlternateNodes(node.next, !isOdd);
}
push(head, 12);
push(head, 29);
push(head, 11);
push(head, 23);
push(head, 8);
sum = 0;
productAlternateNodes(head, true );
document.write(sum);
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- As we are traversing the list only once.
- Auxiliary Space: O(1)
- At first this might look to use O(n) extra space due to recursion call stack but here function is the tail recursive function and thus the compilers optimize it to use constant extra space.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!