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

Posted on 2008-02-08
Medium Priority
Last Modified: 2010-04-21
How do I write a function to remove all nodes with even values in a singly linked list of integers.
Question by:LYRivers
  • 3
  • 2
  • 2
LVL 40

Accepted Solution

evilrix earned 1500 total points
ID: 20852468
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.

LVL 10

Expert Comment

ID: 20852522
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 40

Expert Comment

ID: 20852554
@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?
LVL 10

Expert Comment

ID: 20853013
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

Author Comment

ID: 20854354
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.

Author Closing Comment

ID: 31429266
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.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question