Suma de todos los Nodes distintos en una lista enlazada

Dada una lista enlazada y puede constar de Nodes duplicados. La tarea es encontrar la suma de Nodes no duplicados. 
Ejemplos: 
 

Entrada: 1 -> 2 -> 1 -> 3 -> 4 -> 3 -> NULL 
Salida:
2 y 4 son los únicos Nodes no duplicados y 2 + 4 = 6.
Entrada: 1 -> 3 -> 1 -> 3 -> 1 -> 3 -> NULL 
Salida:
 

Enfoque: Atravesamos toda la lista enlazada y para cada Node verificamos a lo largo de la lista enlazada si el Node tiene duplicados o no. Si no hay un Node duplicado, agregamos ese Node a la variable que almacena la suma.
A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Represents a node of linked list
struct Node {
    int data;
    Node* next;
};
 
// Function to insert node in a linked list
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;
    }
}
 
// Function to find the sum of non duplicate nodes
int sumOfNonDupNode(Node* head)
{
    Node* ptr1 = head;
    Node* ptr2;
    int sum = 0, flag = 0;
 
    while (ptr1 != NULL) {
        ptr2 = head;
        bool flag = false;
 
        // Check if current node has some duplicate
        while (ptr2 != NULL) {
 
            // Check for duplicate node
            if (ptr1 != ptr2 && ptr1->data == ptr2->data) {
                flag = true;
                break;
            }
 
            // Get to the next node
            ptr2 = ptr2->next;
        }
 
        // If current node is unique
        if (!flag)
            sum += ptr1->data;
 
        // Get to the next node
        ptr1 = ptr1->next;
    }
    return sum;
}
 
// Driver code
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

// Java implementation of the approach
class GFG {
 
    // Represents a node of linked list
    static class Node {
        int data;
        Node next;
    }
 
    // Function to insert node in a linked list
    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;
    }
 
    // Function to find the sum of non duplicate nodes
    static int sumOfNonDupNode(Node head)
    {
        Node ptr1 = head;
        Node ptr2;
        int sum = 0;
 
        while (ptr1 != null) {
            ptr2 = head;
            boolean flag = false;
 
            // Check if current node has some duplicate
            while (ptr2 != null) {
 
                // Check for duplicate node
                if (ptr1 != ptr2 && ptr1.data == ptr2.data) {
                    flag = true;
                    break;
                }
 
                // Get to the next node
                ptr2 = ptr2.next;
            }
 
            // If current node is unique
            if (!flag)
                sum += ptr1.data;
 
            // Get to the next node
            ptr1 = ptr1.next;
        }
        return sum;
    }
 
    // Driver code
    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));
    }
}
 
// This code is contributed by Arnab Kundu

Python3

# Python3 implementation of the approach
 
# Represents node of the linked list
class Node:
     
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Insertion in linked list
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
 
# Function to find the sum of
# non duplicate nodes
def sumOfNonDupNode(head):
 
    ptr1 = head
    Sum = flag = 0
 
    while ptr1 != None:
        ptr2, flag = head, False
 
        # Check if current node
        # has some duplicate
        while ptr2 != None:
 
            # Check for duplicate node
            if (ptr1 != ptr2 and
                ptr1.data == ptr2.data):
                flag = True
                break
 
            # Get to the next node
            ptr2 = ptr2.next
         
        # If current node is unique
        if not flag:
            Sum += ptr1.data
 
        # Get to the next node
        ptr1 = ptr1.next
     
    return Sum
 
# Driver code
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))
     
# This code is contributed by Rituraj Jain

C#

// C# implementation of the approach
using System;
 
class GFG {
 
    public class Node {
        public int data;
        public Node next;
    }
 
    // Function to insert node in a linked list
    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;
    }
 
    // Function to find the sum of non duplicate nodes
    static int sumOfNonDupNode(Node head)
    {
        Node ptr1 = head;
        Node ptr2;
        int sum = 0;
 
        while (ptr1 != null) {
            ptr2 = head;
            bool flag = false;
 
            // Check if current node has some duplicate
            while (ptr2 != null) {
 
                // Check for duplicate node
                if (ptr1 != ptr2 && ptr1.data == ptr2.data) {
                    flag = true;
                    break;
                }
 
                // Get to the next node
                ptr2 = ptr2.next;
            }
 
            // If current node is unique
            if (!flag)
                sum += ptr1.data;
 
            // Get to the next node
            ptr1 = ptr1.next;
        }
        return sum;
    }
 
    // Driver code
    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));
    }
}
 
// This code contributed by Rajput-Ji

Javascript

<script>
 
// Javascript implementation of the approach
 
// Represents a node of linked list
class Node {
        constructor(val) {
            this.data = val;
            this.next = null;
         }
     }
 
// Function to insert node in a linked list
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 to find the sum of non duplicate nodes
function sumOfNonDupNode( head)
{
    var ptr1 = head;
    var ptr2;
    var sum = 0;
 
    while (ptr1 != null) {
        ptr2 = head;
        var flag = false;
 
        // Check if current node has some duplicate
        while (ptr2 != null) {
 
            // Check for duplicate node
            if (ptr1 != ptr2 && ptr1.data == ptr2.data) {
                flag = true;
                break;
            }
 
            // Get to the next node
            ptr2 = ptr2.next;
        }
 
        // If current node is unique
        if (!flag)
            sum += ptr1.data;
 
        // Get to the next node
        ptr1 = ptr1.next;
    }
    return sum;
}
 
    // Driver Code
 
    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));
 
// This code is contributed by Jana_sayantan.
</script>
Producción: 

6

 

Publicación traducida automáticamente

Artículo escrito por Premdeep Toppo y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *