Sum of nodes in a linked list which are greater than next node

Given a linked list, the task is to find the sum of all the nodes which are greater than the node next to them. Note that for the last node of the linked list which doesn’t have any node next to it, it must be greater than the first node in order for it to contribute to the sum.

Examples:

Input: 9 -> 2 -> 3 -> 5 -> 4 -> 6 -> 8
Output: 14
9 + 5 = 14



Input: 2 -> 1 -> 5 -> 7
Output: 9
2 + 7 = 9

Approach: Traverse the whole linked list and for each node, if the node is greater than the next node then add it to the sum. For the last node, compare it to the head of the linked list, if last node is greater than the head then add it to the sum. Print the sum in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Represents node of the linked list
struct Node {
    int data;
    Node* next;
};
  
// Function to insert a node at the
// end of the linked list
void insert(Node** root, int item)
{
    Node *ptr = *root, *temp = new Node;
    temp->data = item;
    temp->next = NULL;
  
    if (*root == NULL)
        *root = temp;
    else {
        while (ptr->next != NULL)
            ptr = ptr->next;
        ptr->next = temp;
    }
}
  
// Function to return the sum of the nodes
// which are greater than the node next to them
int sum(Node* root)
{
  
    // If there are no nodes
    if (root == NULL)
        return 0;
  
    int sm = 0;
    Node* ptr = root;
    while (ptr->next != NULL) {
  
        // If the node is greater than the next node
        if (ptr->data > ptr->next->data)
            sm += ptr->data;
        ptr = ptr->next;
    }
  
    // For the last node
    if (ptr->data > root->data)
        sm += ptr->data;
  
    // Return the sum
    return sm;
}
  
// Driver code
int main()
{
    Node* root = NULL;
  
    insert(&root, 9);
    insert(&root, 2);
    insert(&root, 3);
    insert(&root, 5);
    insert(&root, 4);
    insert(&root, 6);
    insert(&root, 8);
  
    cout << sum(root) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG
{
  
// Represents node of the linked list 
static class Node 
    int data; 
    Node next; 
}; 
  
// Function to insert a node at the 
// end of the linked list 
static Node insert(Node root, int item) 
    Node ptr = root, temp = new Node(); 
    temp.data = item; 
    temp.next = null
  
    if (root == null
        root = temp; 
    else 
    
        while (ptr.next != null
            ptr = ptr.next; 
        ptr.next = temp; 
    
    return root;
  
// Function to return the sum of the nodes 
// which are greater than the node next to them 
static int sum(Node root) 
  
    // If there are no nodes 
    if (root == null
        return 0
  
    int sm = 0
    Node ptr = root; 
    while (ptr.next != null
    
  
        // If the node is greater than the next node 
        if (ptr.data > ptr.next.data) 
            sm += ptr.data; 
        ptr = ptr.next; 
    
  
    // For the last node 
    if (ptr.data > root.data) 
        sm += ptr.data; 
  
    // Return the sum 
    return sm; 
  
// Driver code 
public static void main(String args[])
    Node root = null
  
    root=insert(root, 9); 
    root=insert(root, 2); 
    root=insert(root, 3); 
    root=insert(root, 5); 
    root=insert(root, 4); 
    root=insert(root, 6); 
    root=insert(root, 8); 
  
    System.out.print( sum(root) );
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
// Represents node of the linked list 
public class Node 
    public int data; 
    public Node next; 
}; 
  
// Function to insert a node at the 
// end of the linked list 
static Node insert(Node root, int item) 
    Node ptr = root, temp = new Node(); 
    temp.data = item; 
    temp.next = null
  
    if (root == null
        root = temp; 
    else
    
        while (ptr.next != null
            ptr = ptr.next; 
        ptr.next = temp; 
    
    return root;
  
// Function to return the sum of the nodes 
// which are greater than the node next to them 
static int sum(Node root) 
  
    // If there are no nodes 
    if (root == null
        return 0; 
  
    int sm = 0; 
    Node ptr = root; 
    while (ptr.next != null
    
  
        // If the node is greater than the next node 
        if (ptr.data > ptr.next.data) 
            sm += ptr.data; 
        ptr = ptr.next; 
    
  
    // For the last node 
    if (ptr.data > root.data) 
        sm += ptr.data; 
  
    // Return the sum 
    return sm; 
  
// Driver code 
public static void Main(String []args)
    Node root = null
  
    root = insert(root, 9); 
    root = insert(root, 2); 
    root = insert(root, 3); 
    root = insert(root, 5); 
    root = insert(root, 4); 
    root = insert(root, 6); 
    root = insert(root, 8); 
  
    Console.Write( sum(root) );
}
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

14


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234, Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.