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.
LYRiversAsked:
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.

evilrixSenior 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

Your issues matter to us.

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

Start your 7-day free trial
oleberCommented:
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;

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

evilrixSenior 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
oleberCommented:
First of all, I didn't read your answer, before I get my answer

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
LYRiversAuthor 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
LYRiversAuthor 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.