Singly Linked List – Delete Node

You are given a pointer to a node in a singly linked list. Delete that node from the linked list. Pointer to previous node is not available.


The algorithm is as the following:

We have a list looking like: … -> Node(i-1) -> Node(i) -> Node(i+1) -> … and we need to delete Node(i).

Step 1: Copy data (not pointer, the data itself) from Node(i+1) to Node(i), the list will look like: … -> Node(i-1) -> Node(i+1) -> Node(i+1) -> …
Step 2: Copy the NEXT of second Node(i+1) into a temporary variable.
Step 3: Now Delete the second Node(i+1), it doesn’t require pointer to the previous node.

void deleteNode( Node * node )
Node * temp = node->next;
node->data = node->next->data;
node->next = temp->next;

Note: Node should not be the last node.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation