You are given a singly linked list and an integer K. A node in the list is considered "k-balanced" if the absolute difference between the sum of values of all nodes before it and the sum of values of all nodes after it is less than or equal to K.
| (Sum of values before the node) - (Sum of values after the node) | <= K
The sum of nodes before the head of the list is 0. The sum of nodes after the tail of the list is also 0.
Your task is to determine if every node in the linked list is k-balanced. If all nodes satisfy this condition, return true. If even one node fails the check, return false. An empty list or a list with a single node is considered balanced by definition.
Input Format:
The first line contains the node data for the linked list, separated by single spaces. The input is terminated by -1.
The second line contains a single integer K.
Output Format:
Print true if all nodes in the list are k-balanced, and false otherwise.
Note:
The sums can become very large, so be sure to use a 64-bit integer type (like long in Java or long long in C++).