Comments on: Programming Interview Questions 5: Linked List Remove Nodes /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-5-linkedlist-remove-nodes Information Retrieval and Machine Learning Thu, 20 Jun 2013 16:29:00 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Swati Sharma /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-7579 Swati Sharma Wed, 08 May 2013 05:28:00 +0000 /?p=533#comment-7579 Check out these youtube videos on link list and data structure: (), Very well explained!

]]>
By: Fatih /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-7552 Fatih Sat, 02 Mar 2013 13:42:00 +0000 /?p=533#comment-7552 To note, this is the recursive version. I have removed my name accidentally :)

]]>
By: Guest /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-7551 Guest Sat, 02 Mar 2013 13:40:00 +0000 /?p=533#comment-7551 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;

}

]]>
By: rahul /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-7522 rahul Thu, 13 Dec 2012 08:43:00 +0000 /?p=533#comment-7522 while(curr->link!=NULL)

{

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

{

printf(“Inside cond”);

curr->link=next->link;

break;

} else {

curr=curr->link;

next=next->link;

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

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

}

}

]]>
By: Harkirat Saluja /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-7505 Harkirat Saluja Wed, 05 Dec 2012 15:46:00 +0000 /?p=533#comment-7505 #include

struct node
{
int a;
struct node *link;
};

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

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

}
}

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

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

}
temp=temp->link;
temp1=temp1->link;

}

}

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

add(&p,5);
add(&p,6);
add(&p,7);
add(&p,8);
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

]]>
By: Chandu /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-5230 Chandu Sun, 24 Jun 2012 06:47:35 +0000 /?p=533#comment-5230 Good explanation. Explained with different cases for deleting.

]]>
By: sathvik /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-4762 sathvik Wed, 30 May 2012 22:45:28 +0000 /?p=533#comment-4762 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;
}

]]>
By: Sachin /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-1027 Sachin Thu, 29 Dec 2011 04:47:23 +0000 /?p=533#comment-1027
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 }

]]>
By: Sachin /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-1026 Sachin Thu, 29 Dec 2011 04:45:54 +0000 /?p=533#comment-1026 void RemoveNodesWithValue(Node** head, int k) {
Node* toDelete;
while((*head) != NULL) {
if ((*head)->data == k) {
toDelete = (*head);
*head = (*head)-> next;
delete toDelete;
continue;
}
head = &((*head)->next);
}

]]>
By: Arden /2011/09/29/programming-interview-questions-5-linkedlist-remove-nodes/#comment-641 Arden Tue, 29 Nov 2011 17:05:08 +0000 /?p=533#comment-641 Great solution, thanks!

]]>