Given a linked list, the task is to find the balanced node in a linked list. A balanced node is a node where the sum of all the nodes on its left is equal to the sum of all the node on its right, if no such node is found then print -1.
Input: 1 -> 2 -> 7 -> 10 -> 1 -> 6 -> 3 -> NULL
Sum of nodes on the left of 10 is 1 + 2 + 7 = 10
And, to the right of 10 is 1 + 6 + 3 = 10
Input: 1 -> 5 -> 5 -> 10 -> -3 -> NULL
- First, find the total sum of the all node values.
- Now, traverse the linked list one by one and while traversing keep track of all the previous nodes value sum and find the sum of the remaining node by subtracting current node value and the sum of the previous nodes value from the total sum.
- Compare both the sums, if they are equal then current node is the required node else print -1.
Below is the implementation of the above approach:
Time Complexity: O(n)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find extra node in the second Linked list
- Find the fractional (or n/k - th) node in linked list
- Find first node of loop in a linked list
- Find modular node in a linked list
- Recursive Approach to find nth node from the end in the linked list
- Find the second last node of a linked list in single traversal
- Find kth node from Middle towards Head of a Linked List
- Find the largest node in Doubly linked list
- Sorted Linked List to Balanced BST
- Create new linked list from two given linked list with greater element at each node
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Swap Kth node from beginning with Kth node from end in a Linked List
- Remove Nth node from end of the Linked List
- Remove every k-th node of the linked list
- Linked List | Set 2 (Inserting a node)
- Delete Nth node from the end of the given linked list
- Squareroot(n)-th node in a Linked List
- Remove last node of the linked list
- Linked List | Set 3 (Deleting a node)
- Remove first node of the linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.