You are on page 1of 4

#include<bits/stdc++.

h>

using namespace std;

struct node{

long long int data;

node* nextocome;

};

struct node* insert(struct node *head,long long int data)

struct node *t=(struct node *)malloc(sizeof(struct node));

t->data=data;

t->nextocome=NULL;

static struct node *last=NULL; // using static pointer pointing to last


element so that insertion can be done in O(1) time .

if(head==NULL)

head=t;

last=t;

else{

last->nextocome=t;

last=t;

return head;

}
struct node * solve(struct node* head)

struct node *temparature=head,*p=NULL;

while(temparature!=NULL){

if(temparature->data%2==0) // if
element is even

// start is pointing to the previous element to first


occurence of even and end is pointing to next element to the last even.

// start-> 1 2 4 6 4 10 8 12 4 end-> 7 9

struct node* prepost,*nextocome,*start,*end;

prepost=NULL;

start=temparature;

while(temparature!=NULL && temparature->data%2==0)


// loop for reversing the linnked list using 3 pointer.

nextocome=temparature->nextocome;

temparature->nextocome=prepost;

prepost=temparature;

temparature=nextocome;

end=temparature;

start->nextocome=temparature;
if(p==NULL)

head=prepost;

else

p->nextocome=prepost;

else{

p=temparature;

temparature=temparature->nextocome;

return head;

void display(struct node *head)

struct node* temparature=head;

while(temparature!=NULL)

cout<<temparature->data<<" ";

temparature=temparature->nextocome;
}

int main()

int linklist;

cin>>linklist;

long long int temparature;

struct node *head=NULL;

for(int i=0;i<linklist;i++)

cin>>temparature;

head=insert(head,temparature);

head=solve(head);

display(head);

return 0;

You might also like