Even after odd ques

i created a variable which will take all the even no. to last like this

void evenafterodd(node*&head){
if(head==NULL){
return;
}
nodeeven=head;
node
temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
while(even->next!=NULL){
if(even->data%2==0){
temp->next=even;
even->next=NULL;
}
even=even->next;
}
}

but the link will be broken in between for e.g. if lets say the test case is 1 2 2 2 1 so if i am shifting 2 at 1 position in the last the link to next node is broken and it is showing output as 1 1 . So how to create a pointer which will hold the link ?

hey @Vivek-Pandey-2129725577345937 in second while loop you are iterating the array by pointer even and inside loop you are changing the pointer even which will make your code fail.
what you can do is take three pointer odd ,even and a temp .
iterate list with temp and if you found odd element add it to odd and increment odd else add it to even and increment even at last your last odd will point next to the first even. and your first odd will be head of the final list.

https://ide.codingblocks.com/s/179630 i think i am facing the same problem again link is getting broken again
@sdevwrat .

@Vivek-Pandey-2129725577345937 take a look at this for help:

public void evenAfter() {

    Node Even_head = new Node();
    Node Odd_head = new Node();

    Node even = Even_head;
    Node odd = Odd_head;

    Node temp = this.head;

    while(temp != null){

        if(temp.data % 2 == 0){
            even.next = temp;
            even = even.next;
        }else{

            odd.next = temp;
            odd = odd.next;
        }

        temp = temp.next;
    }

    odd.next = Even_head.next;
    this.head = Odd_head.next;
    this.tail = even;
    this.tail.next = null;

}

still if you don’t get https://ide.codingblocks.com/s/180987 refer to this for help