# Deleting nodes having greater value on right side

Problem Summary:

Given a singly linked list, remove all the nodes which have a greater value on right side.

Example: The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side.

Full problem statement

My logic follows Method 2 mentioned in the solution to that problem.

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node* next;
};

{
struct node* current;
for (current = head; current != NULL; current = current->next)
printf ("%d\n",current->data);
}

void appendNode (struct node** headRef, int data)
{
struct node* newNode = malloc(sizeof(struct node));

newNode->data = data;
}

{
struct node* past = NULL;
struct node* present;
struct node* future;

for (present = *head; present != NULL; )
{
future = present->next;
present->next = past;
past = present;
present = future;
}
}

{
struct node* current;
struct node* prev;
int max;

{
for (current = (*head)->next; current != NULL;)
{

if (current->data >= max)
{
max = current->data;
prev = current;
current = current->next;
}
else
{
if (current->next != NULL)
{
struct node* temp = current->next;
current->data = temp->data;
current->next = temp->next;
free (temp);
}
else
{
prev->next = NULL;
free (current);
break;
}
}
}
}
}

int main(void)
{

struct node* tail;
struct node* current;

int input,num,num1;

printf ("Enter no. of inputs \n");
scanf ("%d",&num);
num1 = num;
printf ("\n");
printf ("Enter the numbers\n");

do
{
scanf("%d",&input);
{
}
else
{
appendNode (&tail->next, input);
tail = tail->next;
}
num--;
} while (num > 0);

printf ("\n Before Reversing :\n");

printf ("\n After Reversing :\n");

printf ("\n After Removing :\n");

printf ("\n Final Output :\n");

return 0;
}

• I want to downvote the page where you found the problem :-). The problem is not stated very well. (1) linked list don't have right sides (2) the last line of Example "a" is wrong, 11 is missing. (3) The examples are not very instructive. if you remove an element of a linked list if it points to a higher element, you get the right output. (This was my understanding of the problem before I read the solution) (4) The short solutions are placed immediately after the examples. It is almost not possible not to read them. – miracle173 Aug 29 '14 at 6:36

In appendNode(), you have to provide a pointer to node to sizeof() and typecast its return value to struct node*:

void appendNode (struct node** headRef, int data)
{
struct node* newNode = (struct node*)malloc(sizeof(struct node*));

newNode->data = data;
}

• Welcome to Code Review! There is indeed a bug in the call to malloc(), but you haven't stated the problem exactly in your answer. – 200_success Aug 29 '14 at 4:38

It hink that your removeGreater function can be simplified:

    void removeGreater (struct node** head)
{
while (current)
{
if (current->next && current->data > current->next->data)
{
struct node* remove = current->next;
current->next = remove->next;
free(remove);
}
else
{
current = current->next;
}
}
}