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;
};
//Add new node at end of linked list 
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;
		if ( *head == NULL)
		{
			*head = node;
		}
		else
		{
			struct Node *temp = *head;
			//find last node
			while (temp->next != NULL)
			{
				temp = temp->next;
			}
			//add node at last possition
			temp->next = node;
		}
	}
}
//Display linked list element
void display(struct Node *head)
{
	if (head == NULL)
	{
		printf("Empty linked list");
		return;
	}
	struct Node *temp = head;
	printf("\n Linked List :");
	while (temp != NULL)
	{
		printf("  %d", temp->data);
		temp = temp->next;
		if (temp == head)
		{
			//When loop existing
			return;
		}
	}
}
void find_balanced_node(struct Node *head)
{
	//Define Useful resultant variables
	struct Node *temp = head;
	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
	temp = head;
	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
	struct Node *head1 = NULL;
	//insert element of linked list
	insert( &head1, 1);
	insert( &head1, 2);
	insert( &head1, 3);
	insert( &head1, 4);
	insert( &head1, 2);
	insert( &head1, 1);
	insert( &head1, 3);
	//Display linked list
	display(head1);
	//Find balanced node
	find_balanced_node(head1);
	//create node pointer
	struct Node *head2 = NULL;
	insert( &head2, 1);
	insert( &head2, 3);
	insert( &head2, 6);
	insert( &head2, 1);
	insert( &head2, 1);
	insert( &head2, 1);
	insert( &head2, 1);
	//Display linked list
	display(head2);
	//Find balanced node
	find_balanced_node(head2);
	//create node pointer
	struct Node *head3 = NULL;
	insert( &head3, 1);
	insert( &head3, 2);
	insert( &head3, 3);
	//Display linked list
	display(head3);
	//Find balanced node
	find_balanced_node(head3);
	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
//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int value)
	{
		//Create a node
		Node node = new Node(value);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list nodes
	public void display()
	{
		if (this.head != null)
		{
			System.out.print(" Linked List :");
			Node temp = this.head;
			while (temp != null)
			{
				System.out.print(" " + temp.data);
				temp = temp.next;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			System.out.println("Empty Linked List");
		}
	}
	public void find_balanced_node()
	{
		//Define Useful resultant variables
		Node temp = this.head;
		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
		temp = this.head;
		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)
	{
		MyLinkedList obj1 = new MyLinkedList();
		MyLinkedList obj2 = new MyLinkedList();
		MyLinkedList obj3 = new MyLinkedList();
		//Create first linked list
		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();
		//Second linked list
		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();
		//Third linked list
		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
//Node of LinkedList
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		//set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: Node * head;
	Node * tail;
	//Class constructors
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void insert(int value)
	{
		//Create a node
		Node * node = new Node(value);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Display linked list nodes
	void display()
	{
		if (this->head != NULL)
		{
			cout << " Linked List :";
			Node * temp = this->head;
			while (temp != NULL)
			{
				cout << " " << temp->data;
				temp = temp->next;
				if (temp == this->head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			cout << "Empty Linked List";
		}
	}
	void find_balanced_node()
	{
		//Define Useful resultant variables
		Node * temp = this->head;
		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
		temp = this->head;
		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()
{
	MyLinkedList obj1 =  MyLinkedList();
	MyLinkedList obj2 =  MyLinkedList();
	MyLinkedList obj3 =  MyLinkedList();
	//Create first linked list
	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();
	//Second linked list
	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();
	//Third linked list
	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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int value)
	{
		//Create a node
		Node node = new Node(value);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list nodes
	public void display()
	{
		if (this.head != null)
		{
			Console.Write(" Linked List :");
			Node temp = this.head;
			while (temp != null)
			{
				Console.Write(" " + temp.data);
				temp = temp.next;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			Console.WriteLine("Empty Linked List");
		}
	}
	public void find_balanced_node()
	{
		//Define Useful resultant variables
		Node temp = this.head;
		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
		temp = this.head;
		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)
	{
		MyLinkedList obj1 = new MyLinkedList();
		MyLinkedList obj2 = new MyLinkedList();
		MyLinkedList obj3 = new MyLinkedList();
		//Create first linked list
		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();
		//Second linked list
		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();
		//Third linked list
		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
//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructors
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function insert($value)
	{
		//Create a node
		$node = new Node($value);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Display linked list nodes
	public	function display()
	{
		if ($this->head != null)
		{
			echo " Linked List :";
			$temp = $this->head;
			while ($temp != null)
			{
				echo " ". $temp->data;
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			echo "Empty Linked List";
		}
	}
	public	function find_balanced_node()
	{
		//Define Useful resultant variables
		$temp = $this->head;
		$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
		$temp = $this->head;
		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 = new MyLinkedList();
	$obj2 = new MyLinkedList();
	$obj3 = new MyLinkedList();
	//Create first linked list
	$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();
	//Second linked list
	$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();
	//Third linked list
	$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

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructors
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	insert(value)
	{
		//Create a node
		var node = new Node(value);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list nodes
	display()
	{
		if (this.head != null)
		{
			process.stdout.write(" Linked List :");
			var temp = this.head;
			while (temp != null)
			{
				process.stdout.write(" " + temp.data);
				temp = temp.next;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			process.stdout.write("Empty Linked List");
		}
	}
	find_balanced_node()
	{
		//Define Useful resultant variables
		var temp = this.head;
		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
		temp = this.head;
		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()
{
	var obj1 = new MyLinkedList();
	var obj2 = new MyLinkedList();
	var obj3 = new MyLinkedList();
	//Create first linked list
	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();
	//Second linked list
	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();
	//Third linked list
	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

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

class MyLinkedList :
	
	# Class constructors
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def insert(self, value) :
		# Create a node
		node = Node(value)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Display linked list nodes
	def display(self) :
		if (self.head != None) :
			print(" Linked List :", end = "")
			temp = self.head
			while (temp != None) :
				print(" ", temp.data, end = "")
				temp = temp.next
				if (temp == self.head) :
					# avoid loop
					return
				
			
		else :
			print("Empty Linked List", end = "")
		
	
	def find_balanced_node(self) :
		# Define Useful resultant variables
		temp = self.head
		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
		temp = self.head
		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 = MyLinkedList()
	obj2 = MyLinkedList()
	obj3 = MyLinkedList()
	# Create first linked list
	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()
	# Second linked list
	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()
	# Third linked list
	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

# Node of LinkedList
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :next
	attr_accessor :data, :next
	
	def initialize(data)
	
		# set node value
		self.data = data
		self.next = nil
	end
end
class MyLinkedList 

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail
	
	# Class constructors
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def insert(value)
	
		# Create a node
		node = Node.new(value)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Display linked list nodes
	def display()
	
		if (self.head != nil)
		
			print(" Linked List :")
			temp = self.head
			while (temp != nil)
			
				print(" ", temp.data)
				temp = temp.next
				if (temp == self.head)
				
					# avoid loop
					return
				end
			end
		else
		
			print("Empty Linked List")
		end
	end
	def find_balanced_node()
	
		# Define Useful resultant variables
		temp = self.head
		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
		temp = self.head
		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 = MyLinkedList.new()
	obj2 = MyLinkedList.new()
	obj3 = MyLinkedList.new()
	# Create first linked list
	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()
	# Second linked list
	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()
	# Third linked list
	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

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	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);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list nodes
	def display(): Unit = {
		if (this.head != null)
		{
			print(" Linked List :");
			var temp: Node = this.head;
			while (temp != null)
			{
				print(" " + temp.data);
				temp = temp.next;
				if (temp == this.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			print("Empty Linked List");
		}
	}
	def find_balanced_node(): Unit = {
		//Define Useful resultant variables
		var temp: Node = this.head;
		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
		temp = this.head;
		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 = {
		var obj1: MyLinkedList = new MyLinkedList();
		var obj2: MyLinkedList = new MyLinkedList();
		var obj3: MyLinkedList = new MyLinkedList();
		//Create first linked list
		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();
		//Second linked list
		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();
		//Third linked list
		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

//Node of LinkedList
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		//set node value
		self.data = data;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructors
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//insert node at last of linke list
	func insert(_ value: Int)
	{
		//Create a node
		let node: Node? = Node(value);
		if (self.head == nil)
		{
			//When linked list empty add first node
			self.head = node;
			self.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			self.tail!.next = node;
			self.tail = node;
		}
	}
	//Display linked list nodes
	func display()
	{
		if (self.head != nil)
		{
			print(" Linked List :", terminator: "");
			var temp: Node? = self.head;
			while (temp != nil)
			{
				print(" ", temp!.data, terminator: "");
				temp = temp!.next;
				if (temp === self.head)
				{
					//avoid loop
					return;
				}
			}
		}
		else
		{
			print("Empty Linked List", terminator: "");
		}
	}
	func find_balanced_node()
	{
		//Define Useful resultant variables
		var temp: Node? = self.head;
		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
		temp = self.head;
		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()
{
	let obj1: MyLinkedList = MyLinkedList();
	let obj2: MyLinkedList = MyLinkedList();
	let obj3: MyLinkedList = MyLinkedList();
	//Create first linked list
	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();
	//Second linked list
	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();
	//Third linked list
	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.

New Comment







© 2021, kalkicode.com, All rights reserved