“Write functions Insert and Remove which add and remove nodes from ordered single linked list based on the Node value”


 


Operations of insertion and deletion in linked list require simple pointer change, so modifying recursive list traversal we can
write a solution as following:



Insert:


public Node Insert(Node head, int value)
{
if (head == null || value < head.value)
return new Node(value, head);
else
{
head.next = Insert(head.next, value);
return head;
}
}


Delete:


public Node Remove(Node head, int value)
{
if (head == null)
return null;
else
if (head.value == value)
return head.next;
else
{
head.next = Remove(head.next, value);
return head;
}
}









Answers and Comments