Delete a Particular Character (Linked List)

Photo by Thought Catalog on Unsplash

Before we get into the problem. I have published an ebook. A compilation of 100 Java(Interview)Programming problems which have been solved . I have given clear explanation and the code in the book.

Trust me when I say, this will be a kick start for you to achieve the job at your dream company.

Click on this link to get you to the landing page. It is completely free when you use kindle amazon. Take a look at it.

AIM

Given a LinkedList (where each node has a character) remove the given character

Input: H->e->l->l->o->NULL

CharacterToRemove: ‘l’

Output: H->e->o->NULL

Code

public class Answer implements QuestionInterface {

@Override

public SchNode func(SchNode head, char ch){

SchNode temp,curr;

if(head==null)

return null;

else

{

while(head.ch==ch)

{

head=head.nextNode;

if(head==null)

return null;

}

temp=head;

curr=head;

while(temp!=null)

{

if(temp.ch==ch)

{

curr.nextNode=temp.nextNode;

temp=temp.nextNode;

}

else

{

curr=temp;

temp=temp.nextNode;}

}

return head;

}

}

}

Explanation:

The problem is to return the output list where the given character has to be deleted in the input list.

Using the looping condition the list data that is stored in each and every node is checked. If the node has the data that has the similar character that has to be deleted the node is removed and

The reference of the previous node is made pointed to the next node of the deleted node.

Algorithm:

  • The input list and character to be deleted is passed to the function func(SchNode head, char ch).
  • If the input list head is empty then the null value is returned as output.
  • If the head is null then the head is changed from the current node to next node if the head is empty than the null value is returned as output.
  • If the head does not contain the character that has to be deleted then using the while loop each and every node data till the end of the list is checked.
  • The variable temp and head are assigned with the reference to the head value of the list.
  • If the current node character is similar to the character to be deleted then the the reference of the node to be deleted is changed using curr.nextNode=temp.nextNode and then the other list nodes are traversed using temp=temp.nextNode.
  • The procedure is continued till the end of the list and the final list head is returned as output.

Conclusion

Conclusion

You can follow House of Codes to receive updates when we post new coding challenges. Also you could send us your solutions or your ideas for any coding challenge. We would be thrilled to read them. ;)

Author : Architha Harinath

Editor : Akshay Ravindran

Code -> Understand-> Repeat is my motto. I am a Data Engineer who writes about everything related to Data Science and Interview Preparation for SDE.

Code -> Understand-> Repeat is my motto. I am a Data Engineer who writes about everything related to Data Science and Interview Preparation for SDE.