Take the 2-minute tour ×

I have a pointer to a node, I want to delete that particular node from linked list. The below logic works fine, But it fails if node to be deleted is Last node. How to delete the last node?

void deleteNodWhosePointerIsGivene(node *pointerNode)
  node *temp=pointerNode->next;
share|improve this question

6 Answers 6

First of all you are not removing pointerNode from the list, you are removing the next node in the list. Secondly you are not checking if there is a next node or not.

share|improve this answer
suppose the list is given as 1--->2--->3--->4---->5. I want to delete node containing data '3'. My logic will copy the data '4' in place of data '3' and delete the next node of given pointer. –  aj983 Oct 31 '11 at 9:59
@aj983: You should maybe rephrase the function name as you actually is not removing the "given" node. But the problem you have with it failing if the given node is at the end of the list, you should think about that then pointerNode->next is NULL, and what will happen when you access NULL-pointers. –  Joachim Pileborg Oct 31 '11 at 10:04
That is what I am thinking. If I modify my logic as if(pointerNode->next!= NULL) { node *temp=pointerNode->next; pointerNode->id=temp->id; pointerNode->next=temp->next; free(temp); } then in my else condition I should handle my last node deletion condition. How to handle that. –  aj983 Oct 31 '11 at 10:08
Given your design, you have to find the next to last node, and call deleteNodWhosePointerIsGivene on that node. –  Joachim Pileborg Oct 31 '11 at 10:19
The design is essentially flawed: Since it frees the next node, it cannot possibly delete the last node. The last node has no next node. It's a single-linked list, so you can't just step back. –  MSalters Oct 31 '11 at 10:26

Think about what you are trying to do.

You are trying to delete the next object in the list.

You are at the end of the list, therefore the temp pointer will be null.

You are then trying to get the next from null and also trying to free null.

Normally I would do something along the lines of.

 Get Next Object
 Get Prev Object
 Set Prev->next = Next 
 Set Next->prev = Prev
 Delete obj;

If I am understanding you comments above you are wanting to do a copy then your code should look like.

 Get Next Object
 if (Next Object is not null)
    This.Id == Next.Id
    Free Next
    Throw exception "Cannot delete last object in list"

There is no way to set the pointer in the second to last object in the list if you want to delete the last object in the list. The only way to do this is to either use a double-linked list, or iterate down the list looking for the pointer you wish to delete and keep track of the last object in the list.

share|improve this answer
If we have only pointer given to a node that is to be deleted, then we can get the previous node reference. –  aj983 Oct 31 '11 at 10:02
I am trying to implement in Single Link list. I think your logic is for Doubly Linked List. –  aj983 Oct 31 '11 at 10:03
Fair enough. But then thinking about the object and pointer values at each stage, and figuring out when it will be null. Draw the operation on a bit of paper to help visualise the steps helps. It is what I got students to do when I was tutoring university students. –  Andrew Norman Oct 31 '11 at 10:09
Sorry Unable to understand..I think again some issue is there in code. –  aj983 Oct 31 '11 at 10:09
Thinking about it, you cannot delete the current object, you will have to do a deleteNextObject that throws an exception if you try and delete the last object. Or use a double linked list. –  Andrew Norman Oct 31 '11 at 10:11

You aren't deleting pointerNode in your code, You are deleting pointerNode->next.

Looking at your example of a single linked list. Let's say we have:


You pass in a pointerNode of "3". Your code then does the following:

1) Assigns pointerNode->Next to temp i.e. "4"

2) pointerNode->Next will be assigned temp->Next i.e. "5"

3) temp is freed i.e. "4"

so your list after this would be 1->2->3->5.

When you get to node "5" then you will get an access violation

1) pointerNode->next is assigned to temp i.e. NULL

2) pointerNode->Next will be assigned temp->Next i.e. access violation as you reference a NULL pointer.

A doubly linked list would be a better solution as to delete pointerNode you need to change the Next pointer of the previous node. Otherwise you'd have to scan the list first to find the node prior to pointerNode.

share|improve this answer

Here's the code which should work for a singularly linked list. Bear in mind this will be quite slow for large lists and I would second Andrew Norman's suggestion of using a doubly linked list.

Anyway.. here goes. In order for this to work you need to pass the root node of the list and beware that the address of this node may get changed if you try to delete it, hence I pass it as a pointer to a pointer.

void DeleteNode (Node **rootNode,Node *pointerNode)
    Node *prevNode;

    if (pointerNode == *rootNode) {

       // Head node is being removed

       *rootNode = pointerNode->Next;

    } else {

       // Find the previous node

       for (prevNode = *rootNode; prevNode->Next != pointerNode;
            prevNode = prevNode->Next) ;

       prevNode->Next = pointerNode->Next;

    // Free the node

    free (pointerNode);
share|improve this answer

If you really need to delete the item associated with the node whose pointer you've got, then you have two options when it comes to the last node:

  • Iterate from the beginning of the list to find its predecessor (the next-to-last node), delete the last node and set it's predecessor's next to NULL.

  • Take the lazy approach - do not actually delete the node and only mark it as dead (e.g by setting its data to an impossible value.) Delete it later when you reach it from the predecessor (then also NULL-ing predecessor's next).

Both approaches have obvious drawbacks. This is why it is best to always have the predecessor when deleting a node from a linked list.

share|improve this answer

If you are able to have a reference to the previous node.

void deleteNodWhosePointerIsGivene(structNode* pCurrentNode, structNode* pPreviousNode)
    // Not the Last Node ?
        pPreviousNode->pNext = pCurrentNode->pNext);                    
        pPreviousNode->pNext = NULL;

    delete pCurrentNode;
    pCurrentNode = NULL;

Otherwise you will need to have a reference to the First Node of the List, and search for the previous.

share|improve this answer

Your Answer


