#include
using namespace std;
class node{
public:
int data;
node*next = NULL;
node(int d){
data = d;
next = NULL;
}
};
void insert_at_tail(node*&head , int d){
if(head == NULL)
{
node*n = new node(d);
head = n;
return;
}
node*n = new node(d);
node*temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = n;
n->next = NULL;
return;
}
void print_ll(node*head){
while(head != NULL){
cout << head->data << " " ;
head = head->next;
}
cout<<endl;
}
void checklist(node*head){
if(head == NULL)
return;
node*temp1 = head;
node*temp2 = head->next;
while(temp2 != NULL){
node *temp = temp1->next;
if(temp1->data < temp2->data)
{
if(temp1 == head){
head = temp2;
}
delete temp1;
}
temp2 = temp2->next;
temp1 = temp;
}
print_ll(head);
}
int main () {
node* head = NULL;
int n ;
cin >> n ;
while(n–){
int d;
cin >> d ;
insert_at_tail(head , d);
}
checklist(head);
return 0;
}
why isn’t it working?
ques - to delete nodes where the preceeding element is smaller than its right element