# Programming Interview Questions 5: Linked List Remove Nodes

This is a very fundamental question and it’s tricky to implement without any bugs. Given a linkedlist of integers and an integer value, delete every node of the linkedlist containing that value.

There are many corner cases to consider, here are some. The value to remove is 5.

Inputs are the head of the linked list and the integer value to remove. The most important corner case occurs when the element to delete is at the head of the linkedlist. So the caller’s head pointer should be updated. Because of this, our function should take the head either as a pointer to pointer or pass by reference (or we can also return the new head pointer back to the caller). Otherwise the changes won’t be seen by the caller. I personally prefer pass by reference since it leads to cleaner code.

Let’s say we want to remove all the nodes with the value 5. The solution is first we remove all consecutive fives in the beginning of the linkedlist, and update the head accordingly to point to the first element other than 5 (can be null as well). Then we begin a loop and check the next node whether its value is 5. If it isn’t then we advance the pointer to the next node and continue. If it is 5 then we modify the next pointers accordingly and delete the next note. But we don’t advance our pointer, this is very important. Because the new next node could also contain the value 5, and if we advance the pointer we won’t be able to delete it. This is a subtle corner case which occurs when two or more consecutive nodes should be deleted. Here is the C code:

```typedef struct Node{ int val; Node *next; } Node;   void removeNodes(Node* &amp;head, int rmv) { while (head!=NULL &amp;&amp; head-&gt;val==rmv) { Node *temp=head; head=head-&gt;next; free(temp); } if (head==NULL) return;   Node *current=head; while (current-&gt;next!=NULL) { if (current-&gt;next-&gt;val==rmv) { Node *temp=current-&gt;next; current-&gt;next=temp-&gt;next; free(temp); } else { current=current-&gt;next; } } }```

The time complexity is O(N) and space complexity is O(1), which is optimal. I especially like this interview question because it demonstrates fundamental concepts and it’s not easy to code bug free.

VN:F [1.9.22_1171]
Rating: 7.5/10 (33 votes cast)
Programming Interview Questions 5: Linked List Remove Nodes, 7.5 out of 10 based on 33 ratings
This entry was posted in Programming Interview. Bookmark the permalink.
• Anonymous

you can also merge it in one loop:

list *p = head, *prev = 0, *t;
while(p != 0) {
if(p->val != a) {
prev = p;
p = p->next;
continue;
}
t = p->next;
if(prev != 0) {
prev->next = t;
} else {
}
delete p;
p = t;
}
}

• Arden

Yes but the number of comparisons is more important than the number of loops. At every iteration you check whether the head should be modified or not by prev!=0. This comparison would be true most of the time, because the number of times the removed node will appear in the beginning of the linkedlist is very low compared to the other locations. So most of the time you won’t need to modify the head. And if the linkedlist is long you will keep making this comparison, even after you performed all consecutive removals in the beginning of the linkedlist. So, given a linkedlist of length 1 million which doesn’t contain any 5s, you will make this extra comparison a million times even though it’s not necessary. That’s why I seperated those cases in two different loops, to avoid the extra unnecessary comparisons.

• Anonymous

The following pattern removes the need for special cases.

``` Node* RemoveNodesWithValue(Node* head, int k) { if (!head) { return head; }```

``` Node dummy; dummy.next = head; Node* prev = &dummy; Node* current = head; while (current) { if (current->data == k) { prev->next = current->next; delete current; current = prev->next; } else { prev = current; current = current->next; } } ```

``` return dummy.next; } ```

• Arden

Great solution, thanks!

• Sachin

void RemoveNodesWithValue(Node** head, int k) {
Node* toDelete;
while((*head) != NULL) {
if ((*head)->data == k) {
delete toDelete;
continue;
}
}

• MERON ABRAHA

a very simple approach :

public Node deleteAllDublicate(int value) {

Node node = newHead;

while (node.next != null) {

if (node.next.data == value) {

node.next = node.next.next;

} else {

node = node.next;

}

}

}

public Node clearHead(Node node, int value) {

if (node.data != value) return node;

}

• Sachin

``` 1 void RemoveNodesWithValue(Node** head, int k) { 2 Node* toDelete; 3 while((*head) != NULL) { 4 if ((*head)->data == k) { 5 toDelete = (*head); 6 *head = (*head)-> next; 7 delete toDelete; 8 continue; 9 } 10 head = &((*head)->next); 11 } ```

• sathvik

Using recursion: the call would be head = Delete(head,key)

private Node Delete(Node current, int key)
{
if (current == null) return null;

if (current.key == key)
{
current = current.next;
current = Delete(current, key);
}
else current.next = Delete(current.next, key);

return current;
}

• http://chanduthedev.blogspot.com Chandu

Good explanation. Explained with different cases for deleting.

• http://www.facebook.com/harkirat.saluja Harkirat Saluja

#include

struct node
{
int a;
};

void add(struct node **p,int n)
{
struct node *temp,*r;
temp=*p;
if(*p==NULL)
{
temp=malloc(sizeof(struct node));
temp->a=n;
*p=temp;

}
else
{ temp=*p;
{
}
r=malloc(sizeof(struct node));
r->a=n;

}
}

void display(struct node *a)
{
while(a!=NULL)
{
printf(“%d”,a->a);
}
}

void delete(struct node **a,int n)
{
struct node *temp,*temp1;
temp=*a;
if(temp->a==n)
{
*a=temp1;
}
else
{
{
{

}

}

}

}
void main()
{
struct node *p;
p=NULL;

display(p);
delete(&p,7);
printf(“n”);
display(p);
}

when i am trying this all working fine but last node is not getting deleted… any help

• rahul

{

if((next->a)==n)

{

printf(“Inside cond”);

break;

} else {

printf(“curr->a %d n”, curr->a);

printf(“next->a %d n”, next->a);

}

}

• Guest

Here is the C# code

public ListNode removeNodeWithCertainValue(ListNode firstNode, int value)

{

if (firstNode == null)

{

return null;

}

if (firstNode.NextListNode == null)

{

if(firstNode.NodeValue == value){

return null;

}

return firstNode;

}

//there may be multiple nodes with the given value

while (firstNode!= null && firstNode.NodeValue == value)

{

firstNode = firstNode.NextListNode;

}

//this check is needed in case all nodes have the provided value

if (firstNode != null)

{

firstNode.NextListNode = removeNodeWithCertainValue(firstNode.NextListNode, value);

}

return firstNode;

}

• Fatih

To note, this is the recursive version. I have removed my name accidentally :)

• Swati Sharma

Check out these youtube videos on link list and data structure: (http://www.youtube.com/watch?v=92S4zgXN17o&list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P), Very well explained!

• Nikhil

I don’t understand why we need two while loops. Is the following not a correct implementation?

//DeleteAll
void deleteAll(int data)
{
Node * temp = NULL;
Node * curr = head;
while(curr != NULL)
{
if(curr->data == data)
{
temp = curr->next;
free(curr);
head = curr = temp;
}
else
curr = curr->next;
}
}

• Vishal Hemnani

This code will keep updating head pointer even when the value to be found is not in head pointer.
Include a condition if(curr->data == data && curr == head) (not sure of the C syntax).

Java implementation: private static ListNode Remove(ListNode node, int value)

public class ListNode {
int value;
ListNode node;
private ListNode(int value)
{
this.value=value;
}
private ListNode()
{
}
private ListNode Add(int value)
{
ListNode node = new ListNode(value);
this.node=node;
return node;
}
public static void main(String[] args)
{
ListNode node=new ListNode(1);
System.out.println(“start”);
Print(node);
node=Remove(node,5);
System.out.println();
Print(node);
System.out.println();
System.out.println(“done”);
}
private static ListNode Remove(ListNode node, int value)
{
if(node == null)
return null;
if(node.value == value)
return Remove(node.node,value);
node.node=Remove(node.node,value);
return node;
}
private static void Print(ListNode node)
{
if(node != null)
{
System.out.print(node.value + ” “);
Print(node.node);
}
}
}

Java implementation:

public void removeNode(Node head, int toRemove) {

Node previous = null;
Node current = head;

while(current != null) {

if (current.data == toRemove) {
if (current == head) {
} else {
previous.next = current.next;
}
} else {
previous = current;
}

current = current.next;
}
}

• So Sta

This is probably a stupid question but could you explain the conditional in the first while loop “head!=NULL & amp ; & amp; head->val==rmv”? What is gt, and what is the purpose of “& amp ; & amp”? Thank you very much!