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++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
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;
}
}
int sum(Node* root)
{
if (root == NULL)
return 0;
int sm = 0;
Node* ptr = root;
while (ptr->next != NULL) {
if (ptr->data > ptr->next->data)
sm += ptr->data;
ptr = ptr->next;
}
if (ptr->data > root->data)
sm += ptr->data;
return sm;
}
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;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
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;
}
static int sum(Node root)
{
if (root == null )
return 0 ;
int sm = 0 ;
Node ptr = root;
while (ptr.next != null )
{
if (ptr.data > ptr.next.data)
sm += ptr.data;
ptr = ptr.next;
}
if (ptr.data > root.data)
sm += ptr.data;
return sm;
}
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) );
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(root, item):
ptr = root
temp = Node(item);
temp.data = item;
temp. next = None ;
if (root = = None ):
root = temp;
else :
while (ptr. next ! = None ):
ptr = ptr. next ;
ptr. next = temp;
return root
def sum (root):
if (root = = None ):
return 0 ;
sm = 0 ;
ptr = root;
while (ptr. next ! = None ):
if (ptr.data > ptr. next .data):
sm + = ptr.data;
ptr = ptr. next ;
if (ptr.data > root.data):
sm + = ptr.data;
return sm;
if __name__ = = '__main__' :
root = None ;
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 );
print ( sum (root))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
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;
}
static int sum(Node root)
{
if (root == null )
return 0;
int sm = 0;
Node ptr = root;
while (ptr.next != null )
{
if (ptr.data > ptr.next.data)
sm += ptr.data;
ptr = ptr.next;
}
if (ptr.data > root.data)
sm += ptr.data;
return sm;
}
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) );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function insert( root, item)
{
var 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 sum( root)
{
if (root == null )
return 0;
let sm = 0;
var ptr = root;
while (ptr.next != null )
{
if (ptr.data > ptr.next.data)
sm += ptr.data;
ptr = ptr.next;
}
if (ptr.data > root.data)
sm += ptr.data;
return sm;
}
var 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);
document.write( sum(root) );
</script>
|
Time complexity: O(n) where n is the size of the linked list
Auxiliary Space: O(1)
Last Updated :
20 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...