# Find sum of even and odd nodes in a linked list

Here given code implementation process.

``````// C Program
// Find sum of even and odd nodes in a linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function

{
int data;
};
// Singly linked list
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->tail = NULL;
}
return sll;
}
// Returns a new Node of linked list
{
// Create dynamic node
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
//Add new Node at end of linked list
void addNode(struct SingleLL *sll, int data)
{
if (sll->head == NULL)
{
sll->tail = node;
}
else
{
// Append the node at last position
sll->tail->next = node;
sll->tail = node;
}
}
// Display linked list element
void display(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != NULL)
{
if (temp != sll->head)
{
printf(" →");
}
printf(" %d", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" → NULL\n");
}
// Calculate Sum of all even and odd node in a linked list
void nodeSum(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
// This is used to collect sum result
int evenSum = 0;
int oddSum = 0;
// Get first node
// Sum node values
while (temp != NULL)
{
if (temp->data % 2 == 0)
{
// When node contains Even value
evenSum += temp->data;
}
else
{
// When node contains Odd value
oddSum += temp->data;
}
// Visit to next node
temp = temp->next;
}
// Display node elements
display(sll);
// Display the value of calculated sum
printf(" Even Sum : %d", evenSum);
printf("\n Odd Sum : %d\n", oddSum);
}
int main()
{
// Create a empty linked list
struct SingleLL *sll = newLinkedList();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
nodeSum(sll);
return 0;
}``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````/*
Java Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
public int data;
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
if (this.head == null)
{
this.tail = node;
}
else
{
// Append the node at last position
this.tail.next = node;
this.tail = node;
}
}
// Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
System.out.print(" →");
}
System.out.print(" " + temp.data + "");
// Visit to next node
temp = temp.next;
}
System.out.print(" → NULL\n");
}
// Calculate Sum of all even and odd node in a linked list
public void nodeSum()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
// This is used to collect sum result
int evenSum = 0;
int oddSum = 0;
// Get first node
// Sum node values
while (temp != null)
{
if (temp.data % 2 == 0)
{
// When node contains Even value
evenSum += temp.data;
}
else
{
// When node contains Odd value
oddSum += temp.data;
}
// Visit to next node
temp = temp.next;
}
// Display node elements
this.display();
// Display the value of calculated sum
System.out.print(" Even Sum : " + evenSum );
System.out.print("\n Odd Sum : " + oddSum + "\n");
}
public static void main(String[] args)
{
SingleLL sll = new SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
}
}``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};;
class SingleLL
{
SingleLL()
{
this->tail = NULL;
}
//Add new Node at end of linked list
{
if (this->head == NULL)
{
this->tail = node;
}
else
{
// Append the node at last position
this->tail->next = node;
this->tail = node;
}
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
//iterating linked list elements
while (temp != NULL)
{
if (temp != this->head)
{
cout << " →";
}
cout << " " << temp->data << "";
// Visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
// Calculate Sum of all even and odd node in a linked list
void nodeSum()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
// This is used to collect sum result
int evenSum = 0;
int oddSum = 0;
// Get first node
// Sum node values
while (temp != NULL)
{
if (temp->data % 2 == 0)
{
// When node contains Even value
evenSum += temp->data;
}
else
{
// When node contains Odd value
oddSum += temp->data;
}
// Visit to next node
temp = temp->next;
}
// Display node elements
this->display();
// Display the value of calculated sum
cout << " Even Sum : " << evenSum;
cout << "\n Odd Sum : " << oddSum << "\n";
}
};
int main()
{
SingleLL sll = SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
return 0;
}``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````// Include namespace system
using System;
/*
C# Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
public int data;
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
if (this.head == null)
{
this.tail = node;
}
else
{
// Append the node at last position
this.tail.next = node;
this.tail = node;
}
}
// Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
Console.Write(" →");
}
Console.Write(" " + temp.data + "");
// Visit to next node
temp = temp.next;
}
Console.Write(" → NULL\n");
}
// Calculate Sum of all even and odd node in a linked list
public void nodeSum()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
// This is used to collect sum result
int evenSum = 0;
int oddSum  = 0;
// Get first node
// Sum node values
while (temp != null)
{
if (temp.data % 2 == 0)
{
// When node contains Even value
evenSum += temp.data;
}
else
{
// When node contains Odd value
oddSum += temp.data;
}
// Visit to next node
temp = temp.next;
}
// Display node elements
this.display();
// Display the value of calculated sum
Console.Write(" Even Sum : " + evenSum);
Console.Write("\n Odd Sum : " + oddSum + "\n");
}
public static void Main(String[] args)
{
SingleLL sll = new SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
}
}``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````<?php
/*
Php Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
};
class SingleLL
{
public \$tail;

function __construct()
{
\$this->tail = null;
}
//Add new Node at end of linked list
{
\$node = new LinkNode(\$data);
if (\$this->head == null)
{
\$this->tail = \$node;
}
else
{
// Append the node at last position
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
// Display linked list element
public	function display()
{
if (\$this->head == null)
{
echo "\n Empty linked list\n";
return;
}
//iterating linked list elements
while (\$temp != null)
{
if (\$temp != \$this->head)
{
echo " →";
}
echo " ". \$temp->data ."";
// Visit to next node
\$temp = \$temp->next;
}
echo " → NULL\n";
}
// Calculate Sum of all even and odd node in a linked list
public	function nodeSum()
{
if (\$this->head == null)
{
echo "\n Empty linked list\n";
return;
}
// This is used to collect sum result
\$evenSum = 0;
\$oddSum = 0;
// Get first node
// Sum node values
while (\$temp != null)
{
if (\$temp->data % 2 == 0)
{
// When node contains Even value
\$evenSum += \$temp->data;
}
else
{
// When node contains Odd value
\$oddSum += \$temp->data;
}
// Visit to next node
\$temp = \$temp->next;
}
// Display node elements
\$this->display();
// Display the value of calculated sum
echo " Even Sum : ". \$evenSum;
echo "\n Odd Sum : ". \$oddSum ."\n";
}
}

function main()
{
\$sll = new SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
\$sll->nodeSum();
}
main();``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````/*
Node Js Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
constructor(data)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
constructor()
{
this.tail = null;
}
//Add new Node at end of linked list
{
var node = new LinkNode(data);
if (this.head == null)
{
this.tail = node;
}
else
{
// Append the node at last position
this.tail.next = node;
this.tail = node;
}
}
// Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
var temp = this.head;
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data + "");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\n");
}
// Calculate Sum of all even and odd node in a linked list
nodeSum()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
// This is used to collect sum result
var evenSum = 0;
var oddSum = 0;
// Get first node
var temp = this.head;
// Sum node values
while (temp != null)
{
if (temp.data % 2 == 0)
{
// When node contains Even value
evenSum += temp.data;
}
else
{
// When node contains Odd value
oddSum += temp.data;
}
// Visit to next node
temp = temp.next;
}
// Display node elements
this.display();
// Display the value of calculated sum
process.stdout.write(" Even Sum : " + evenSum);
process.stdout.write("\n Odd Sum : " + oddSum + "\n");
}
}

function main()
{
var sll = new SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
}
main();``````

#### Output

``````  2  →  5  →  1  →  8  →  10  →  4  →  9  →  7  → NULL
Even Sum :  24
Odd Sum :  22``````
``````#   Python 3 Program
#   Find sum of even and odd nodes in a linked list

#  Linked list node

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :

def __init__(self) :
self.tail = None

# Add new Node at end of linked list
def addNode(self, data) :
if (self.head == None) :
self.tail = node
else :
#  Append the node at last position
self.tail.next = node
self.tail = node

#  Display linked list element
def display(self) :
if (self.head == None) :
print("\n Empty linked list")
return

# iterating linked list elements
while (temp != None) :
if (temp != self.head) :
print(" →", end = "")

print(" ", temp.data ,"", end = "")
#  Visit to next node
temp = temp.next

print(" → NULL")

#  Calculate Sum of all even and odd node in a linked list
def nodeSum(self) :
if (self.head == None) :
print("\n Empty linked list")
return

#  This is used to collect sum result
evenSum = 0
oddSum = 0
#  Get first node
#  Sum node values
while (temp != None) :
if (temp.data % 2 == 0) :
#  When node contains Even value
evenSum += temp.data
else :
#  When node contains Odd value
oddSum += temp.data

#  Visit to next node
temp = temp.next

#  Display node elements
self.display()
#  Display the value of calculated sum
print(" Even Sum : ", evenSum, end = "")
print("\n Odd Sum : ", oddSum )

def main() :
sll = SingleLL()
#   2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum()

if __name__ == "__main__": main()``````

#### Output

``````  2  →  5  →  1  →  8  →  10  →  4  →  9  →  7  → NULL
Even Sum :  24
Odd Sum :  22``````
``````#   Ruby Program
#   Find sum of even and odd nodes in a linked list

#  Linked list node
# Define the accessor and reader of class LinkNode
attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL

def initialize()
self.tail = nil
end

# Add new Node at end of linked list
if (self.head == nil)
self.tail = node
else
#  Append the node at last position
self.tail.next = node
self.tail = node
end

end

#  Display linked list element
def display()
if (self.head == nil)
print("\n Empty linked list\n")
return
end

# iterating linked list elements
while (temp != nil)
if (temp != self.head)
print(" →")
end

print(" ", temp.data ,"")
#  Visit to next node
temp = temp.next
end

print(" → NULL\n")
end

#  Calculate Sum of all even and odd node in a linked list
def nodeSum()
if (self.head == nil)
print("\n Empty linked list\n")
return
end

#  This is used to collect sum result
evenSum = 0
oddSum = 0
#  Get first node
#  Sum node values
while (temp != nil)
if (temp.data % 2 == 0)
#  When node contains Even value
evenSum += temp.data
else
#  When node contains Odd value
oddSum += temp.data
end

#  Visit to next node
temp = temp.next
end

#  Display node elements
self.display()
#  Display the value of calculated sum
print(" Even Sum : ", evenSum)
print("\n Odd Sum : ", oddSum ,"\n")
end

end

def main()
sll = SingleLL.new()
#   2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum()
end

main()``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22
``````
``````/*
Scala Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
def this(data: Int)
{
this(data, null);
}
};
{
def this()
{
this(null, null);
}
//Add new Node at end of linked list
def addNode(data: Int): Unit = {
if (this.head == null)
{
this.tail = node;
}
else
{
// Append the node at last position
this.tail.next = node;
this.tail = node;
}
}
// Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
print(" →");
}
print(" " + temp.data + "");
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Calculate Sum of all even and odd node in a linked list
def nodeSum(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// This is used to collect sum result
var evenSum: Int = 0;
var oddSum: Int = 0;
// Get first node
// Sum node values
while (temp != null)
{
if (temp.data % 2 == 0)
{
// When node contains Even value
evenSum += temp.data;
}
else
{
// When node contains Odd value
oddSum += temp.data;
}
// Visit to next node
temp = temp.next;
}
// Display node elements
this.display();
// Display the value of calculated sum
print(" Even Sum : " + evenSum);
print("\n Odd Sum : " + oddSum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
}
}``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````/*
Kotlin Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
var tail: LinkNode ? ;
constructor()
{
this.tail = null;
}
//Add new Node at end of linked list
fun addNode(data: Int): Unit
{
if (this.head == null)
{
this.tail = node;
}
else
{
// Append the node at last position
this.tail?.next = node;
this.tail = node;
}
}
// Display linked list element
fun display(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
print(" →");
}
print(" " + temp.data + "");
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Calculate Sum of all even and odd node in a linked list
fun nodeSum(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// This is used to collect sum result
var evenSum: Int = 0;
var oddSum: Int = 0;
// Get first node
// Sum node values
while (temp != null)
{
if (temp.data % 2 == 0)
{
// When node contains Even value
evenSum += temp.data;
}
else
{
// When node contains Odd value
oddSum += temp.data;
}
// Visit to next node
temp = temp.next;
}
// Display node elements
this.display();
// Display the value of calculated sum
print(" Even Sum : " + evenSum);
print("\n Odd Sum : " + oddSum + "\n");
}
}
fun main(args: Array < String > ): Unit
{
var sll: SingleLL = SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
}``````

#### Output

`````` 2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
Even Sum : 24
Odd Sum : 22``````
``````/*
Swift 4 Program
Find sum of even and odd nodes in a linked list
*/
// Linked list node
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
};
class SingleLL
{
var tail: LinkNode? ;
init()
{
self.tail = nil;
}
//Add new Node at end of linked list
func addNode(_ data: Int)
{
if (self.head == nil)
{
self.tail = node;
}
else
{
// Append the node at last position
self.tail!.next = node;
self.tail = node;
}
}
// Display linked list element
func display()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
//iterating linked list elements
while (temp  != nil)
{
if (!(temp === self.head))
{
print(" →", terminator: "");
}
print(" ", temp!.data ,"", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" → NULL");
}
// Calculate Sum of all even and odd node in a linked list
func nodeSum()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
// This is used to collect sum result
var evenSum: Int = 0;
var oddSum: Int = 0;
// Get first node
// Sum node values
while (temp  != nil)
{
if (temp!.data % 2 == 0)
{
// When node contains Even value
evenSum += temp!.data;
}
else
{
// When node contains Odd value
oddSum += temp!.data;
}
// Visit to next node
temp = temp!.next;
}
// Display node elements
self.display();
// Display the value of calculated sum
print(" Even Sum : ", evenSum, terminator: "");
print("\n Odd Sum : ", oddSum );
}
}
func main()
{
let sll: SingleLL = SingleLL();
//  2 → 5 → 1 → 8 → 10 → 4 → 9 → 7 → NULL
sll.nodeSum();
}
main();``````

#### Output

``````  2  →  5  →  1  →  8  →  10  →  4  →  9  →  7  → NULL
Even Sum :  24
Odd Sum :  22``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.