# linked list Remove a node

• 11-08-2012
espnstar
Does this seem right? I am removing an integer node from the linked list.

Code:

```LNode*  RemoveItem(LNode  *headPtr, int  targetItem) {     struct LNode *trailPtr;     for (headPtr = LNode; headPtr != NULL; headPtr = headPtr ->next)         {         if (headPtr ->value == targetItem)             {             if (headPtr == LNode)                 {                 LNode = headPtr ->next;                 }             else                 {                 trailPtr ->next = headPtr ->next;                 break;                 }             trailPtr = headPtr;             }         } return headPtr; }  // end of "RemoveItem"```
• 11-08-2012
std10093
Code:

`headPtr = LNode`
What does this line do?You assign the headPtr in your type of struct?
It is like doing with an integer for example
Code:

```int a; int * b b = int; /* Not what you want */```
When we handle lists we use as a stop condition(in order to traverse through the list)
Code:

```while(listPointer != NULL) {           /* Your logic here */           listPointer = listPointer->next; /* Go to the next node */ }```
The code above assumes that your struct has a member that is a pointer to list, and it called next, which is pretty usual in linked lists.

PS - i suspect this is C code, not C++
The last node of the list has no next node, so it's member next should be set to NULL.
If the list is empty ,then we set the pointer of the list to NULL.

After you understand what i said ,pick up a pencil and a paper and draw a list with four nodes.Try to remove the 2nd node of it.See what you do in paper with your pointers and do it in your code.Then try to do the same for the first node and again watch what you did in paper.Same story for last node.

Try your code, and if needed post back :)