Given a linked list, the task is to find the sum of distances between the two nearest perfect squares for all the nodes of the given linked list.

**Examples:**

Input:3 -> 15 -> 7 -> NULL

Output:15

For 3: closest left perfect square is 1 and closest right 4 i.e. 4-1 = 3

For 15: 16 – 9 = 7

For 7: 9 – 4 = 5

3 + 7 + 5 = 15

Input:1 -> 5 -> 10 -> 78 -> 23 -> NULL

Output:38

**Approach:** Initialise **sum = 0** and for every node, if the current node’s value is a perfect square itself then the left and right closest perfect square will be the value itself and distance will be 0. Else, find the left and right closest perfect squares say **leftPS** and **rightPS** and update **sum = sum + (rightPS – leftPS)**.

Below is the implementation of the above approach:

`# Python3 implementation of the approach ` `import` `sys ` `import` `math ` ` ` `# Structure for a node ` `class` `Node: ` ` ` `def` `__init__(` `self` `, data): ` ` ` `self` `.data ` `=` `data ` ` ` `self` `.` `next` `=` `None` ` ` `# Function to find the total distance sum ` `def` `distanceSum(head): ` ` ` ` ` `# If head is null ` ` ` `if` `not` `head: ` ` ` `return` ` ` ` ` `# To store the required sum ` ` ` `tsum ` `=` `0` ` ` `temp ` `=` `head ` ` ` ` ` `# Traversing through all the nodes one by one ` ` ` `while` `(temp): ` ` ` `sq_root ` `=` `math.sqrt(temp.data) ` ` ` ` ` `# If current node is not a perfect square ` ` ` `# then find left perfect square and ` ` ` `# right perfect square ` ` ` `if` `sq_root<temp.data: ` ` ` `left_ps ` `=` `math.floor(sq_root)` `*` `*` `2` ` ` `right_ps ` `=` `math.ceil(sq_root)` `*` `*` `2` ` ` `tsum` `+` `=` `(right_ps ` `-` `left_ps) ` ` ` ` ` `# Get to the next node ` ` ` `temp ` `=` `temp.` `next` ` ` `return` `tsum ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `head ` `=` `Node(` `3` `) ` ` ` `head.` `next` `=` `Node(` `15` `) ` ` ` `head.` `next` `.` `next` `=` `Node(` `7` `) ` ` ` `head.` `next` `.` `next` `.` `next` `=` `Node(` `40` `) ` ` ` `head.` `next` `.` `next` `.` `next` `.` `next` `=` `Node(` `42` `) ` ` ` ` ` `result ` `=` `distanceSum(head) ` ` ` `print` `(` `"{}"` `.` `format` `(result)) ` |

*chevron_right*

*filter_none*

**Output:**

41

**Time Complexity:** O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete N nodes after M nodes of a linked list
- Sum of all perfect numbers present in an Linked list
- Linked List Sum of Nodes Between 0s
- Sum and Product of all the nodes which are less than K in the linked list
- Sum of all odd frequency nodes of the Linked List
- Find sum of even and odd nodes in a linked list
- Sum of the alternate nodes of linked list
- Linked List Product of Nodes Between 0s
- Segregate even and odd nodes in a Linked List
- Sum of all distinct nodes in a linked list
- Sum of the nodes of a Singly Linked List
- Find the sum of last n nodes of the given Linked List
- Sum of the nodes of a Circular Linked List
- Print nodes of linked list at given indexes
- Append the last M nodes to the beginning of the given linked list
- Replace nodes with duplicates in linked list
- Alternate Odd and Even Nodes in a Singly 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 contribute@geeksforgeeks.org. 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.