Sum of all distinct nodes in a linked list
Given a linked list and it may consist of duplicate nodes. The task is to find the sum of non-duplicate nodes.
Examples:
Input: 1 -> 2 -> 1 -> 3 -> 4 -> 3 -> NULL
Output: 6
2 and 4 are the only non-duplicate nodes and 2 + 4 = 6.
Input: 1 -> 3 -> 1 -> 3 -> 1 -> 3 -> NULL
Output: 0
Approach: We traverse the whole linked list and for each node we check throughout the linked list whether the node has duplicate or not. If there is no duplicate node then we add that node to the variable which stores the sum.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insert(Node** head, int item)
{
Node* ptr = *head;
Node* temp = new Node;
temp->data = item;
temp->next = NULL;
if (*head == NULL)
*head = temp;
else {
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
int sumOfNonDupNode(Node* head)
{
Node* ptr1 = head;
Node* ptr2;
int sum = 0, flag = 0;
while (ptr1 != NULL) {
ptr2 = head;
bool flag = false ;
while (ptr2 != NULL) {
if (ptr1 != ptr2 && ptr1->data == ptr2->data) {
flag = true ;
break ;
}
ptr2 = ptr2->next;
}
if (!flag)
sum += ptr1->data;
ptr1 = ptr1->next;
}
return sum;
}
int main()
{
Node* head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 1);
insert(&head, 3);
insert(&head, 4);
insert(&head, 3);
cout << sumOfNonDupNode(head) << endl;
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
}
static Node insert(Node head, int item)
{
Node ptr = head;
Node temp = new Node();
temp.data = item;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return head;
}
static int sumOfNonDupNode(Node head)
{
Node ptr1 = head;
Node ptr2;
int sum = 0 ;
while (ptr1 != null ) {
ptr2 = head;
boolean flag = false ;
while (ptr2 != null ) {
if (ptr1 != ptr2 && ptr1.data == ptr2.data) {
flag = true ;
break ;
}
ptr2 = ptr2.next;
}
if (!flag)
sum += ptr1.data;
ptr1 = ptr1.next;
}
return sum;
}
public static void main(String args[])
{
Node head = null ;
head = insert(head, 1 );
head = insert(head, 2 );
head = insert(head, 1 );
head = insert(head, 3 );
head = insert(head, 4 );
head = insert(head, 3 );
System.out.print(sumOfNonDupNode(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(head, item):
ptr = head
temp = Node(item)
if head = = None :
head = temp
else :
while ptr. next ! = None :
ptr = ptr. next
ptr. next = temp
return head
def sumOfNonDupNode(head):
ptr1 = head
Sum = flag = 0
while ptr1 ! = None :
ptr2, flag = head, False
while ptr2 ! = None :
if (ptr1 ! = ptr2 and
ptr1.data = = ptr2.data):
flag = True
break
ptr2 = ptr2. next
if not flag:
Sum + = ptr1.data
ptr1 = ptr1. next
return Sum
if __name__ = = "__main__" :
head = None
head = insert(head, 1 )
head = insert(head, 2 )
head = insert(head, 1 )
head = insert(head, 3 )
head = insert(head, 4 )
head = insert(head, 3 )
print (sumOfNonDupNode(head))
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
}
static Node insert(Node head, int item)
{
Node ptr = head;
Node temp = new Node();
temp.data = item;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return head;
}
static int sumOfNonDupNode(Node head)
{
Node ptr1 = head;
Node ptr2;
int sum = 0;
while (ptr1 != null ) {
ptr2 = head;
bool flag = false ;
while (ptr2 != null ) {
if (ptr1 != ptr2 && ptr1.data == ptr2.data) {
flag = true ;
break ;
}
ptr2 = ptr2.next;
}
if (!flag)
sum += ptr1.data;
ptr1 = ptr1.next;
}
return sum;
}
public static void Main(String[] args)
{
Node head = null ;
head = insert(head, 1);
head = insert(head, 2);
head = insert(head, 1);
head = insert(head, 3);
head = insert(head, 4);
head = insert(head, 3);
Console.Write(sumOfNonDupNode(head));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function insert( head, item)
{
let ptr = head;
var temp = new Node();
temp.data = item;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return head;
}
function sumOfNonDupNode( head)
{
var ptr1 = head;
var ptr2;
var sum = 0;
while (ptr1 != null ) {
ptr2 = head;
var flag = false ;
while (ptr2 != null ) {
if (ptr1 != ptr2 && ptr1.data == ptr2.data) {
flag = true ;
break ;
}
ptr2 = ptr2.next;
}
if (!flag)
sum += ptr1.data;
ptr1 = ptr1.next;
}
return sum;
}
let head = null ;
head = insert(head, 1);
head = insert(head, 2);
head = insert(head, 1);
head = insert(head, 3);
head = insert(head, 4);
head = insert(head, 3);
document.write(sumOfNonDupNode(head));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
20 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...