Delete nodes which have a greater value on right side

Given a singly linked list, remove all the nodes which have a greater value on right side.
Examples:
a) 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.

Comments

  1. void arrange(struct node **ps)
    {
    struct node *temp=*ps;
    struct node *deleteMe;
    while(temp->next)
    {
    if(deleteMe=temp->datanext->data?temp->next:NULL)
    {
    temp->data=temp->next->data;
    temp->next=temp->next->next;
    free(deleteMe);
    }
    temp=temp->next;
    }
    }

    ReplyDelete
  2. @ankur please note that the data is in unsorted state so even if next element is not greater than current element,this does not mean there are no elements greater than current element on right of it..:)

    ReplyDelete

Post a Comment

Popular posts from this blog

Circular game survival