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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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

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

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

    newNode-&gt;data = data;
    newNode-&gt;next = *headRef;
    *headRef = newNode;
}   

void reverseList (struct node** head)
{
    struct node* past = NULL;
    struct node* present;
    struct node* future;

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

void removeGreater (struct node** head)
{
    struct node* current;
    struct node* prev;
    int max;

    if (*head != NULL)
    {
        max = (*head)-&gt;data;
        prev = *head;
        for (current = (*head)-&gt;next; current != NULL;)
        {

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

int main(void)
{

    struct node* head = NULL;
    struct node* tail;
    struct node* current;

    int input,num,num1;

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

    do
    {
        scanf("%d",&amp;input);
        if (head == NULL)
        {
            appendNode (&amp;head, input);
            tail = head;
        }
        else
        {
            appendNode (&amp;tail-&gt;next, input);
            tail = tail-&gt;next;
        }   
        num--;
    } while (num &gt; 0);

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

    reverseList(&amp;head);

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

    removeGreater (&amp;head);
    printf ("\n After Removing :\n");
    printList (head);

    reverseList(&amp;head);
    printf ("\n Final Output :\n");
    printList (head);


    return 0;
}
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-&gt;data = data;
    newNode-&gt;next = *headRef;
    *headRef = newNode;
}