Sum of all distinct nodes in a linked list

Given a linked list, Find the sum of all distinct nodes of this linked list. That is simple problem but comparing one node to other and finding unique nodes this process are tack O(n^2) time.

Suppose we are inserted the following (4, 5, 2, 3, 5, 7) node in a sequence.

Sum of distinct nodes

Here given code implementation process.

The time complexity in the above program is O(n^2). We can optimize this by using set collection. Put the all node value into set, and sum the set element.



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