# Find the balanced node in a linked list

Here given code implementation process.

``````// C Program
// Find the balanced node in a linked list
#include <stdio.h>
//for malloc function
#include <stdlib.h>

//Create structure
struct Node
{
int data;
struct Node *next;
};
void insert(struct Node **head, int value)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
node->data = value;
node->next = NULL;
{
}
else
{
//find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
temp = temp->next;
{
//When loop existing
return;
}
}
}
{
//Define Useful resultant variables
struct Node *result = NULL;
//Define Useful calculations variables
int total_sum = 0;
int current_sum = 0;
//Sum of all nodes
while (temp != NULL)
{
//Calculate sum of node value
total_sum += temp->data;
//Visit to next node
temp = temp->next;
}
//Get first node of linked list
while (temp != NULL && result == NULL)
{
if (total_sum - (current_sum + temp->data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp->data;
//Visit to next node
temp = temp->next;
}
if (result != NULL)
{
printf("\n Balanced node is : %d\n", result->data);
}
else
{
printf("\n Balanced node are not exist \n");
}
}
int main()
{
//create node pointer
//Find balanced node
//create node pointer
//Find balanced node
//create node pointer
//Find balanced node
return 0;
}``````

#### Output

`````` Linked List :  1  2  3  4  2  1  3
Balanced node is : 4

Linked List :  1  3  6  1  1  1  1
Balanced node is : 6

Linked List :  1  2  3
Balanced node are not exist``````
``````// Java Program
// Find the balanced node in a linked list
class Node
{
public int data;
public Node next;
public Node(int data)
{
//set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int value)
{
//Create a node
Node node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
while (temp != null)
{
System.out.print(" " + temp.data);
temp = temp.next;
{
//avoid loop
return;
}
}
}
else
{
}
}
public void find_balanced_node()
{
//Define Useful resultant variables
Node result = null;
//Define Useful calculations variables
int total_sum = 0;
int current_sum = 0;
//Sum of all nodes
while (temp != null)
{
//Calculate sum of node value
total_sum += temp.data;
//Visit to next node
temp = temp.next;
}
//Get first node of linked list
while (temp != null && result == null)
{
if (total_sum - (current_sum + temp.data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp.data;
//Visit to next node
temp = temp.next;
}
if (result != null)
{
System.out.print("\n Balanced node is : " + result.data + "\n");
}
else
{
System.out.print("\n Balanced node not exist \n");
}
}
public static void main(String[] args)
{
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(4);
obj1.insert(2);
obj1.insert(1);
obj1.insert(3);
//Display node elements
obj1.display();
//Find balanced node
obj1.find_balanced_node();
obj2.insert(1);
obj2.insert(3);
obj2.insert(6);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
//Display node elements
obj2.display();
//Find balanced node
obj2.find_balanced_node();
obj3.insert(1);
obj3.insert(2);
obj3.insert(3);
//Display node elements
obj3.display();
//Find balanced node
obj3.find_balanced_node();
}
}``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist``````
``````//Include header file
#include <iostream>
//use namespace
using namespace std;
// C++ Program
// Find the balanced node in a linked list
class Node
{
public: int data;
Node * next;
Node(int data)
{
//set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int value)
{
//Create a node
Node * node = new Node(value);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
cout << " Linked List :";
while (temp != NULL)
{
cout << " " << temp->data;
temp = temp->next;
{
//avoid loop
return;
}
}
}
else
{
}
}
void find_balanced_node()
{
//Define Useful resultant variables
Node * result = NULL;
//Define Useful calculations variables
int total_sum = 0;
int current_sum = 0;
//Sum of all nodes
while (temp != NULL)
{
//Calculate sum of node value
total_sum += temp->data;
//Visit to next node
temp = temp->next;
}
//Get first node of linked list
while (temp != NULL && result == NULL)
{
if (total_sum - (current_sum + temp->data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp->data;
//Visit to next node
temp = temp->next;
}
if (result != NULL)
{
cout << "\n Balanced node is : " << result->data << "\n";
}
else
{
cout << "\n Balanced node not exist \n";
}
}
};
int main()
{
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(4);
obj1.insert(2);
obj1.insert(1);
obj1.insert(3);
//Display node elements
obj1.display();
//Find balanced node
obj1.find_balanced_node();
obj2.insert(1);
obj2.insert(3);
obj2.insert(6);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
//Display node elements
obj2.display();
//Find balanced node
obj2.find_balanced_node();
obj3.insert(1);
obj3.insert(2);
obj3.insert(3);
//Display node elements
obj3.display();
//Find balanced node
obj3.find_balanced_node();
return 0;
}``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist``````
``````//Include namespace system
using System;

// C# Program
// Find the balanced node in a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int value)
{
//Create a node
Node node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
while (temp != null)
{
Console.Write(" " + temp.data);
temp = temp.next;
{
//avoid loop
return;
}
}
}
else
{
}
}
public void find_balanced_node()
{
//Define Useful resultant variables
Node result = null;
//Define Useful calculations variables
int total_sum = 0;
int current_sum = 0;
//Sum of all nodes
while (temp != null)
{
//Calculate sum of node value
total_sum += temp.data;
//Visit to next node
temp = temp.next;
}
//Get first node of linked list
while (temp != null && result == null)
{
if (total_sum - (current_sum + temp.data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp.data;
//Visit to next node
temp = temp.next;
}
if (result != null)
{
Console.Write("\n Balanced node is : " + result.data + "\n");
}
else
{
Console.Write("\n Balanced node not exist \n");
}
}
public static void Main(String[] args)
{
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(4);
obj1.insert(2);
obj1.insert(1);
obj1.insert(3);
//Display node elements
obj1.display();
//Find balanced node
obj1.find_balanced_node();
obj2.insert(1);
obj2.insert(3);
obj2.insert(6);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
//Display node elements
obj2.display();
//Find balanced node
obj2.find_balanced_node();
obj3.insert(1);
obj3.insert(2);
obj3.insert(3);
//Display node elements
obj3.display();
//Find balanced node
obj3.find_balanced_node();
}
}``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist``````
``````<?php
// Php Program
// Find the balanced node in a linked list
class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$value)
{
//Create a node
\$node = new Node(\$value);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
while (\$temp != null)
{
echo " ". \$temp->data;
\$temp = \$temp->next;
{
//avoid loop
return;
}
}
}
else
{
}
}
public	function find_balanced_node()
{
//Define Useful resultant variables
\$result = null;
//Define Useful calculations variables
\$total_sum = 0;
\$current_sum = 0;
//Sum of all nodes
while (\$temp != null)
{
//Calculate sum of node value
\$total_sum += \$temp->data;
//Visit to next node
\$temp = \$temp->next;
}
//Get first node of linked list
while (\$temp != null && \$result == null)
{
if (\$total_sum - (\$current_sum + \$temp->data) == \$current_sum)
{
//When current node is balanced node
\$result = \$temp;
}
//Calculate node sum
\$current_sum += \$temp->data;
//Visit to next node
\$temp = \$temp->next;
}
if (\$result != null)
{
echo "\n Balanced node is : ". \$result->data ."\n";
}
else
{
echo "\n Balanced node not exist \n";
}
}
}

function main()
{
\$obj1->insert(1);
\$obj1->insert(2);
\$obj1->insert(3);
\$obj1->insert(4);
\$obj1->insert(2);
\$obj1->insert(1);
\$obj1->insert(3);
//Display node elements
\$obj1->display();
//Find balanced node
\$obj1->find_balanced_node();
\$obj2->insert(1);
\$obj2->insert(3);
\$obj2->insert(6);
\$obj2->insert(1);
\$obj2->insert(1);
\$obj2->insert(1);
\$obj2->insert(1);
//Display node elements
\$obj2->display();
//Find balanced node
\$obj2->find_balanced_node();
\$obj3->insert(1);
\$obj3->insert(2);
\$obj3->insert(3);
//Display node elements
\$obj3->display();
//Find balanced node
\$obj3->find_balanced_node();
}
main();``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist``````
``````// Node Js Program
// Find the balanced node in a linked list

class Node
{
constructor(data)
{
//set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(value)
{
//Create a node
var node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
while (temp != null)
{
process.stdout.write(" " + temp.data);
temp = temp.next;
{
//avoid loop
return;
}
}
}
else
{
}
}
find_balanced_node()
{
//Define Useful resultant variables
var result = null;
//Define Useful calculations variables
var total_sum = 0;
var current_sum = 0;
//Sum of all nodes
while (temp != null)
{
//Calculate sum of node value
total_sum += temp.data;
//Visit to next node
temp = temp.next;
}
//Get first node of linked list
while (temp != null && result == null)
{
if (total_sum - (current_sum + temp.data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp.data;
//Visit to next node
temp = temp.next;
}
if (result != null)
{
process.stdout.write("\n Balanced node is : " + result.data + "\n");
}
else
{
process.stdout.write("\n Balanced node not exist \n");
}
}
}

function main()
{
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(4);
obj1.insert(2);
obj1.insert(1);
obj1.insert(3);
//Display node elements
obj1.display();
//Find balanced node
obj1.find_balanced_node();
obj2.insert(1);
obj2.insert(3);
obj2.insert(6);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
//Display node elements
obj2.display();
//Find balanced node
obj2.find_balanced_node();
obj3.insert(1);
obj3.insert(2);
obj3.insert(3);
//Display node elements
obj3.display();
//Find balanced node
obj3.find_balanced_node();
}
main();``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist``````
``````#  Python 3 Program
#  Find the balanced node in a linked list

class Node :

def __init__(self, data) :
# set node value
self.data = data
self.next = None

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, value) :
# Create a node
node = Node(value)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print(" Linked List :", end = "")
while (temp != None) :
print(" ", temp.data, end = "")
temp = temp.next
# avoid loop
return

else :
print("Empty Linked List", end = "")

def find_balanced_node(self) :
# Define Useful resultant variables
result = None
# Define Useful calculations variables
total_sum = 0
current_sum = 0
# Sum of all nodes
while (temp != None) :
# Calculate sum of node value
total_sum += temp.data
# Visit to next node
temp = temp.next

# Get first node of linked list
while (temp != None and result == None) :
if (total_sum - (current_sum + temp.data) == current_sum) :
# When current node is balanced node
result = temp

# Calculate node sum
current_sum += temp.data
# Visit to next node
temp = temp.next

if (result != None) :
print("\n Balanced node is : ", result.data ,"\n", end = "")
else :
print("\n Balanced node not exist \n", end = "")

def main() :
obj1.insert(1)
obj1.insert(2)
obj1.insert(3)
obj1.insert(4)
obj1.insert(2)
obj1.insert(1)
obj1.insert(3)
# Display node elements
obj1.display()
# Find balanced node
obj1.find_balanced_node()
obj2.insert(1)
obj2.insert(3)
obj2.insert(6)
obj2.insert(1)
obj2.insert(1)
obj2.insert(1)
obj2.insert(1)
# Display node elements
obj2.display()
# Find balanced node
obj2.find_balanced_node()
obj3.insert(1)
obj3.insert(2)
obj3.insert(3)
# Display node elements
obj3.display()
# Find balanced node
obj3.find_balanced_node()

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

#### Output

`````` Linked List :  1  2  3  4  2  1  3
Balanced node is :  4
Linked List :  1  3  6  1  1  1  1
Balanced node is :  6
Linked List :  1  2  3
Balanced node not exist``````
``````#  Ruby Program
#  Find the balanced node in a linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(value)

# Create a node
node = Node.new(value)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

while (temp != nil)

print(" ", temp.data)
temp = temp.next

# avoid loop
return
end
end
else

end
end
def find_balanced_node()

# Define Useful resultant variables
result = nil
# Define Useful calculations variables
total_sum = 0
current_sum = 0
# Sum of all nodes
while (temp != nil)

# Calculate sum of node value
total_sum += temp.data
# Visit to next node
temp = temp.next
end
# Get first node of linked list
while (temp != nil && result == nil)

if (total_sum - (current_sum + temp.data) == current_sum)

# When current node is balanced node
result = temp
end
# Calculate node sum
current_sum += temp.data
# Visit to next node
temp = temp.next
end
if (result != nil)

print("\n Balanced node is : ", result.data ,"\n")
else

print("\n Balanced node not exist \n")
end
end
end
def main()

obj1.insert(1)
obj1.insert(2)
obj1.insert(3)
obj1.insert(4)
obj1.insert(2)
obj1.insert(1)
obj1.insert(3)
# Display node elements
obj1.display()
# Find balanced node
obj1.find_balanced_node()
obj2.insert(1)
obj2.insert(3)
obj2.insert(6)
obj2.insert(1)
obj2.insert(1)
obj2.insert(1)
obj2.insert(1)
# Display node elements
obj2.display()
# Find balanced node
obj2.find_balanced_node()
obj3.insert(1)
obj3.insert(2)
obj3.insert(3)
# Display node elements
obj3.display()
# Find balanced node
obj3.find_balanced_node()
end
main()``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist
``````
``````// Scala Program
// Find the balanced node in a linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(value: Int): Unit = {
//Create a node
var node: Node = new Node(value);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
while (temp != null)
{
print(" " + temp.data);
temp = temp.next;
{
//avoid loop
return;
}
}
}
else
{
}
}
def find_balanced_node(): Unit = {
//Define Useful resultant variables
var result: Node = null;
//Define Useful calculations variables
var total_sum: Int = 0;
var current_sum: Int = 0;
//Sum of all nodes
while (temp != null)
{
//Calculate sum of node value
total_sum += temp.data;
//Visit to next node
temp = temp.next;
}
//Get first node of linked list
while (temp != null && result == null)
{
if (total_sum - (current_sum + temp.data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp.data;
//Visit to next node
temp = temp.next;
}
if (result != null)
{
print("\n Balanced node is : " + result.data + "\n");
}
else
{
print("\n Balanced node not exist \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(4);
obj1.insert(2);
obj1.insert(1);
obj1.insert(3);
//Display node elements
obj1.display();
//Find balanced node
obj1.find_balanced_node();
obj2.insert(1);
obj2.insert(3);
obj2.insert(6);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
//Display node elements
obj2.display();
//Find balanced node
obj2.find_balanced_node();
obj3.insert(1);
obj3.insert(2);
obj3.insert(3);
//Display node elements
obj3.display();
//Find balanced node
obj3.find_balanced_node();
}
}``````

#### Output

`````` Linked List : 1 2 3 4 2 1 3
Balanced node is : 4
Linked List : 1 3 6 1 1 1 1
Balanced node is : 6
Linked List : 1 2 3
Balanced node not exist``````
``````// Swift Program
// Find the balanced node in a linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ value: Int)
{
//Create a node
let node: Node? = Node(value);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
print(" Linked List :", terminator: "");
while (temp != nil)
{
print(" ", temp!.data, terminator: "");
temp = temp!.next;
{
//avoid loop
return;
}
}
}
else
{
}
}
func find_balanced_node()
{
//Define Useful resultant variables
var result: Node? = nil;
//Define Useful calculations variables
var total_sum: Int = 0;
var current_sum: Int = 0;
//Sum of all nodes
while (temp != nil)
{
//Calculate sum of node value
total_sum += temp!.data;
//Visit to next node
temp = temp!.next;
}
//Get first node of linked list
while (temp != nil && result == nil)
{
if (total_sum - (current_sum + temp!.data) == current_sum)
{
//When current node is balanced node
result = temp;
}
//Calculate node sum
current_sum += temp!.data;
//Visit to next node
temp = temp!.next;
}
if (result != nil)
{
print("\n Balanced node is : ", result!.data ,"\n", terminator: "");
}
else
{
print("\n Balanced node not exist \n", terminator: "");
}
}
}
func main()
{
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(4);
obj1.insert(2);
obj1.insert(1);
obj1.insert(3);
//Display node elements
obj1.display();
//Find balanced node
obj1.find_balanced_node();
obj2.insert(1);
obj2.insert(3);
obj2.insert(6);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
obj2.insert(1);
//Display node elements
obj2.display();
//Find balanced node
obj2.find_balanced_node();
obj3.insert(1);
obj3.insert(2);
obj3.insert(3);
//Display node elements
obj3.display();
//Find balanced node
obj3.find_balanced_node();
}
main();``````

#### Output

`````` Linked List :  1  2  3  4  2  1  3
Balanced node is :  4
Linked List :  1  3  6  1  1  1  1
Balanced node is :  6
Linked List :  1  2  3
Balanced node not exist``````

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.