in the even after odd question of linked list, the code’s giving segmentation fault https://ide.codingblocks.com/s/236446
Even after odd (linked list)
in your else loop, your pointer manipulation is very dangerous, you are losing identity of many pointers and risking a NULL pointer exception
correct approach:
The idea is to split the linked list into two: one containing all even nodes and other containing all odd nodes. And finally attach the odd node linked list after the even node linked list.
To split the Linked List, traverse the original Linked List and move all odd nodes to a separate Linked List of all odd nodes. At the end of loop, the original list will have all the even nodes and the odd node list will have all the odd nodes. To keep the ordering of all nodes same, we must insert all the odd nodes at the end of the odd node list. And to do that in constant time, we must keep track of last pointer in the odd node list.
okay, follow this approach
take four pointers and initialise all to NULL
oddend and evenend to store the ends of even number and odd numbers
and oddstart and evenstart to store the beginning of them
check the first node if it is odd or even and point oddstart or evenstart accordingly
now for temp = head->next start running a loop
in the loop:
if an odd number is encountered and if oddstart is NULL point oddstart to it else if oddstart->next==NULL point odd end else point oddend->next to this temp and move oddend to it next
similarly follow the simlar steps for even numbers
at the end of the loop point oddend to even start and return oddstart as your new head
this is the most optimum one
another one u can follow is in this code