 ## Linked list Insert and delete ## Post Outline

• Code for the linked list insert operation
• Delete operation in linked list
• Code for the linked list delete operation
• Linked list delete operation code explanation
• Quick Rap

There is a linked list with five nodes. 99,100,101,102,103. The values are A,B,C,E,F. We are adding 200 node with value D. Then 200add after 101 node. Because D comes after C.101 node’s current next value = 102

200 node’s current next value = null

101 node’s new next value = 200

200 node’s new next value = 102

### Code for the linked list insert operation

`public void insert(char el){`
`if(head==null)`
`head=tail=new Node(el);`
`else`
`if(el<head.info)`
`head = new Node(el,head);`
`else`
`Node temp,pred;`
`for(pred=head,temp=head.next;temp!=null&&el<temp.info;pred=pred.next,temp=temp.next);`
`if(temp!=null)`
`pred.next=new Node(el,temp);`
`else`
`tail.next= new Node(el);`
`tail=tail.next;`
`}`

Current head will next node after the head. If the element is greater than the head info then it find where should it put. For that like in the deletion process extra nodes will use. In the deletion we use one node call temp. But in here we use two nodes call pred and temp. Pred will take the head value. Temp will take head.next value. For above example,

Pred = 99

Temp =100

Node value = D

1 st comparison = B>D =False

New pred =100

New temp = 101

2 nd Comparison = C>D = false

New pred = 101

New temp = 102

3 rd comparison = E>D = true

Loop will end this point since condition met. El > head.info condition met. If temp is not null means there are more nodes in the linked list. So the new node won’t be the tail node. Which means a highest value not in the current linked list. So it should add after the pred node. 101 node contains value C. So pred.next = new node address. New node next value will be temp. That’s why we use two nodes here. We need to find before and after nodes. So we have to use two nodes to find it.

## Delete operation in linked list

When deleting a node a node will removed from the linked list. After removing the node next values need to be updated. The previous node next value should take the address of the after node to the deleting node. As an example.

There are five nodes in a linked list. 99,100,101,102,103. If we are deleting 101 node then,

Deleting node = 101

100 node’s next value = 101

100 node’s next value after deletion = 102

Linked list after deletion = 99,100,102,103

### Code for the linked list delete operation

`public char delete(char el){`
`if(head==tail && el==head.info)`
`head=tail=null;`
`else`
`if(el==head.info)`
`head=head.next;`
`else`
`Node temp,pred;`
`for(pred=head,temp=head.next;temp!=null&&temp.info!=el;pred=pred.next,temp=temp.next);`
`if(temp!=null)`
`pred.next=temp.next;`
`if(temp==tail)`
`tail=pred;`
`return el;`
`}`

### Linked list delete operation code explanation

This is also same as other deletion. We can delete a node from anywhere. Firstly we check whether both head and tail values are same and head.info equals to the deleting element. If so tail and head values are same means there are no any nodes in the list. Only one node is there. Therefore delete that node and make head and tail values null.

If there are many nodes and if we are deleting the head node. Then it will check whether element is equal to the head.info. That equality means we told to delete the head node. Then current head will removed and head.next node will be new head. So head node value will take previous head.next node’s address.

If we delete from delete like insert we need to find what are beside nodes. So that like insertion we have to use two extra special nodes call pred and temp. Pred will give the previous node and temp will give the after node. Let’s see an example.

If there are five nodes 99,100,101,102,103. Values of the nodes are A,B,C,D,E. If we want to delete 101 node. 101 node contains element C.

1 st comparision = B=C = false

New pred = 100

New temp = 101

2 nd comparison = C=C = true

Now exit from the loop since condition met.

Pred.next will take the temp.next vale. That means 100 node’s next value= 102. Because now 102 node is after 100 node. There is not 101 node it was deleted

## Quick Rap

• We use two extra nodes for singly list insert and delete operations.
• They are pred and temp.
• We are using them to identify previous and after nodes.
• We use self for loop for  insert and delete operations.
• In doubly linked list we don’t need two nodes for this operations.

## Conclusion

From here I am concluding linked list insert and delete operation post. If you like this please comment and let us know your feedback. We will meet again with another interesting topic. Until goodbye all.

### Audy Ranathunga

Audy Ranathunga, Author of myexamnote is our most experienced author. She has been working as a blog post writer for 4 years. She joined with myexamnote before 1 year ago and she has contribute lots of valuable posts for readers.

### This Post Has 2 Comments

1. Himanshu

Well explained. I like so much the way you explained. Very simple to understand.

1. Audy Ranathunga

Thank you Himanshu.