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.

Here given code implementation process.
-
1) Sum of unique elements in linked list in c
2) Sum of unique elements in linked list in java
3) Sum of unique elements in linked list in c++
4) Sum of unique elements in linked list in golang
5) Sum of unique elements in linked list in c#
6) Sum of unique elements in linked list in vb.net
7) Sum of unique elements in linked list in php
8) Sum of unique elements in linked list in node js
9) Sum of unique elements in linked list in typescript
10) Sum of unique elements in linked list in python
11) Sum of unique elements in linked list in ruby
12) Sum of unique elements in linked list in scala
13) Sum of unique elements in linked list in swift
14) Sum of unique elements in linked list in kotlin
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