#include <iostream>
#include <cmath> // For std::abs

// Definition of a Node in the linked list
struct Node {
    int data;
    Node* next;
    
    Node(int val) : data(val), next(nullptr) {}
};

// Function to calculate the sum of absolute differences of all pairs
int sumOfAbsoluteDifferences(Node* root) {
    if (!root) return 0; // If the list is empty
    
    int sum = 0;
    Node* ptr1 = root;
    
    while (ptr1) {
        Node* ptr2 = ptr1->next;
        while (ptr2) {
            sum += std::abs(ptr1->data - ptr2->data);
            ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
    
    return sum;
}

// Utility function to add a node at the end of the list
void appendNode(Node*& head, int value) {
    if (!head) {
        head = new Node(value);
        return;
    }
    Node* temp = head;
    while (temp->next) temp = temp->next;
    temp->next = new Node(value);
}

// Utility function to print a linked list
void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "NULL\n";
}

int main() {
    // Example usage
    Node* head = nullptr;
    appendNode(head, 3.5);
    appendNode(head, 7.3);
    
    
    printList(head); // Output: 1 -> 3 -> 5 -> NULL
    
    int result = sumOfAbsoluteDifferences(head);
    std::cout << "Sum of absolute differences: " << result << std::endl; // Output: 8
    
    return 0;
}