# Delete a node in Linked List

0 comment

Delete is one of the most common operations done in any Linked List. The idea behind deleting a node is pretty simple: identify the node to be deleted and update the pointers. Just like inserting a node, we can delete a node from 3 different places. We also need to make sure that the deleted nodes are completely abandoned such that they are cleaned automatically by the language specific garbage collector.

#### Deleting a node from beginning:

Let us consider a sample linked list with HEAD pointing at 4. We can traverse to the next node by accessing the next pointer of the first node.

A single linked list is always identified by the head node. We can traverse all the elements if we have access to the first node. If our first node points at the second node, there is no way to reach the original node again. This way we can easily delete the node from the beginning. Just go to the next node.

#### Delete a node from the end:

Once again, let us take a sample linked list. The last node of a single linked list is identified by the NEXT pointer pointing to NULL.

If we change the next of first node to NULL, that would remove all the nodes.

If we change the next of second node to NULL, that will just keep the first two nodes.

Hence, to delete the last node from the linked list, we can just mark the NEXT pointer of second last node to NULL.

#### Delete a node from middle:

To remove the nodes from beginning and end, we followed two steps. Identifying the node and then updating the pointers. It is relatively easy to find the head node and last node but to find the node in middle we need to iterate over the list.

Let us take a sample list once again and we will delete the 3rd node.

We can follow the following steps:

• Choose a temp variable to iterate over the list
• Reach the 3rd position in the list
• Point the next of node previous to 3rd node to the next of 3rd node.

This will ultimately delete the node from the linked list. You need not worry about actually deleting the value from the list, as this will be cleaned up automatically by the language you are using to code.

#### Code:

.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
.hljs > mark.shcb-loc { background-color: #ddf6ff; }  public ListNode deleteFromBeginning(ListNode head) {

// Just move the head to the next position

}

// Move to the second last node
while (ptr.next.next != null)
ptr = ptr.next;

// Point the next of second last node to null
ptr.next = null;

}

public ListNode deleteFromMiddle(ListNode head, int position) {

// Move to previous position of node to delete
for (int i = 0; i < position - 1; i++) {
ptr = ptr.next;
}

// Get the node to delete
ListNode nodeToDelete = ptr.next;

// Get the address of node next to the node to be deleted
ListNode nextNode = nodeToDelete.next;

// Point the next of ptr to nextNode
ptr.next = nextNode;

Code language: Java (java)