# How do I write a function in a singly linked list of integers

How do I write a function to remove all nodes with even values in a singly linked list of integers.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Senior Software Engineer (Avast)Commented:
Your function will need to traverse the list from start to finish. For each node it will need to check the next node to see if it's a even value (cur->next->value % 2 == 0 means it's even) and if it is the node will need to be removed. This is done by firstly saving a temporary pointer to the next node, linking the current node with the one after the the next (so cur->next is set to point to cur->next->next) and then finally deleting the node pointed to by the temporary pointer. Then traverse to the next node and repeat.

I've not provided any code at this time as you don't clarify whether this is an assignment or not.

-Rx.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Which is the structure of the singly linked?

Something like this shall work

``````SinglyLinkedList * pointer = basePointer;
while (pointer->next != NULL ) {
if (is_odd(pointer->next->value)) {
pointer->next = pointer->next->next;
}
}

return is_odd(basePointer->value) ? basePointer->next : basePointer;
``````
0
Senior Software Engineer (Avast)Commented:
0
Senior Software Engineer (Avast)Commented:
@oleber, which bit of, "I've not provided any code at this time as you don't clarify whether this is an assignment or not." did you struggle with?

BTW: Won't your code leak memory since you don't preserve pointer->next before overwriting it with pointer->next->next?
0
Commented:

I wrote 'Something like this shall work' since this isn't the final code, is more a pseudocode

and he also has to check if basePointer is null at the beggining
0
Author Commented:
Hi evilrix & oleber.  Thanks very much for the information.  It's not an assignment as I'm not a student -- I was many years ago, though!  I have noted in my profile why I'm asking certain questions.  Thanks for the prompt responses.
0
Author Commented:
The information was enough to add to a 2nd post and help me get closer to solving the problem.  I currently work as a tester and program in SQL and limited VB script.  I'm preparing for a technical interview and this is one of the problems that I was told to study.  Thanks for your assistance.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.