Insert at a Specified Location (Linked List) JAVA

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. Believe me when I say, this will kick start 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) insert a given character at the specified location

Input : H->e->l->l->o->NULL Location: 3 (if the location is beyond LinkedList length, ignore) CharacterToInsert: ‘m’

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

Code

The problem is to insert the new element into the existing linked list at a particular index position.

The entire insertion operation is performed based on the count of the number of nodes in the input linked list.

If the count is less than the index then the original list is returned as output.

If the index is one then the new character is made as the head and if the index is greater than the cunt then the new character is inserted at the end.

If it does not satisfy any of the above conditions then the linked list is traversed and the new character is inserted in the specific index position.

  • The head of the input linked list , the new character to be inserted and the position where the character has to inserted is passed to the function func(SchNode head, int index, char ch)
  • The new variable count is declared and initialized as zero
  • The temporary variable for the node is created and the character to be inserted is stored in that variable newNode, temp, old.
  • The count of number of nodes in the input linked list is calculated by traversing through the list till the end of the list.
  • If the index position is greater than the number of nodes+2 then the original list head is returned as output.
  • If the index value is equal to one then the head of the list is assigned to newNode’s nextnode and then the newNode is made as head. Then the manipulated head is returned as output.
  • If the index is equal to count+1 then the list is traversed till the end of the list and new node is added to the end of the list and the head is returned as output.
  • If the index is anywhere between the count 2 or size of the entire list then the list is traversed through the list and temp is assigned to old and temp’s nextnode is assigned to temp until the index position is reached.
  • Once the index is reached the newnode where the character is stored is assigned to nextnode of old and temp is assigned to nextnode of newnode. Then the head is returned as output.

Conclusion

Well guys, I thank you for all your support.I live by the motto Anybody could code. All it takes is some hard work. And a guide to clear your doubts. Without the help of my mentor, I would not be able to become a coder. He cleared my doubts whenever I had them. And He will not give the answer directly but he will make you understand the logic behind the question.

Whenever you guys have a doubt, regarding these problems. Share your Thoughts with me. I would do my best to clear those doubts.

No Single Solution exists to a problem. Work on the Code.

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: 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.