Link to home
Start Free TrialLog in
Avatar of LYRivers
LYRiversFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

@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?
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
Avatar of LYRivers

ASKER

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