Golang Program to Delete the Kth Node from a Linked List


April 12, 2022, Learn eTutorial
1482

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

A linked list is a basic data structure that consists of a bunch of nodes and each node has two attributes such as data and pointer next. Here the Next is a pointer to the next node in the list. There are so many linked list operations that allow us to perform different actions on a linked list. For example, the deletion operation will delete a node from the linked list.

In this program, we will create a singly linked list and delete the Kth node from the linked list. To accomplish this task, we will find the kth node by traversing through the list till the head become nil. If we found the kth node in the list then update the linked list by deleting the kth node. Let’s see this program in more detail.

How to delete the Kth node

In this program, we’ll focus our attention on how to delete the kth node from a given linked list by using GO language.
There is an fmt package which used to import some standard libraries into the program. Use the built-in function fmt.println() to print anything on the screen. We need to import the “fmt” package to use this function. After that, open the main function. We are doing the whole program inside the main function, which is the starting point of our program.

Here we use a structure to define a node. The function NewNode creates nodes of the linked lists by assigning data into its node value and changing the pointer of the node as the next node. Now the head is the starting node of the linked list. 

The function TraverseLinkedList(head) displays all the nodes from the linked list by traversing a list that starts with the head until it finds the reference value as nil.

Whenever you want to delete the kth node from the given linked list and call the function DeleteKthNode(head,k). In this function, it defines a method that accepts the head of a linked list and the input value that you want to delete. Checks, whether the head is equal to nil which means the list is empty. If the head is equal to nil, return the head without deleting any node. Otherwise, update the linked list by deleting the kth node. For this, traverse through the linked list to find the position of the kth node. If it found kth node then override that node’s next value with its next node’s next value. If node value is not found kth node then return the head without deleting any node.

Finally, you can display the updated linked list by calling the function TraverseLinkedList.

Given below are the steps that are used in the Go program to delete the kth node from the linked list.

ALGORITHM:

STEP 1: Start
STEP 2: Define a structure of the node
STEP 3: Create a linked list by calling the function NewNode(). Here the head is the start node of the linked list.
STEP 4: Display input linked list by Calling the function TraverseLinkedList(head).
STEP 5: delete the kth node from the linked list by calling DeleteKthNode(head, k)
STEP 6: Display the linked list after deleting the node by calling the function TraverseLinkedList(head).
STEP 7: Exit

Steps to Implement NewNode(value):

STEP 1: Declare the variable n as Node.
STEP 2: Create node n by assigning data into its node value and changing the pointer of the node as the next node. 
STEP 3: Return node n

Steps to Implement TraverseLinkedList(head):

STEP 1: Display all the nodes from the linked list by traversing a list that starts with the head until we find the reference value is nil.

Steps to Implement DeleteKthNode(head,k):

STEP 1: Define a method that accepts the head of a linked list.
STEP 2: Checks, whether the head is equal to nil which means the list is empty. If the head is equal to nil, return the head without deleting any node. Otherwise do step 3.
STEP 3: Define a node temp which will initially point to the head of the list.
STEP 4: Traverse through the list till temp points to nil.
STEP 5: In each iteration, check whether temp will point to the kth node. If so, override that node’s next value with its next node’s next value. Otherwise, return the head without deleting any node.
 

Golang Source Code

                                          package main
import "fmt"
type Node struct {
   value int
   next *Node
}
func NewNode(value int, next *Node) *Node{
   var n Node
   n.value = value
   n.next = next
   return &n
}
func TraverseLinkedList(head *Node){
   temp := head
   for temp != nil {
      fmt.Printf("%d ", temp.value)
      temp = temp.next
   }
   fmt.Println()
}
func DeleteAfterKthNode(head *Node, k int) *Node{
   // Delete after Kth node.
   if head == nil{
      return head
   }
   temp := head
   for temp != nil{
      if temp.value == k{
         temp.next = temp.next.next
      }
      temp = temp.next
   }
   return head
}
func main(){
   head := NewNode(320, NewNode(110, NewNode(402, NewNode(503, nil))))
   fmt.Printf("Input Linked list is: ")
   TraverseLinkedList(head)
   head = DeleteAfterKthNode(head, 402)
   fmt.Printf("Node %d is deleted.\nUpdated Linked List after deletion is:\n", 402)
   TraverseLinkedList(head)
}

                                      

OUTPUT

Input Linked list is: 320 110 402 503 
Node 402 is deleted.
Updated Linked List after deletion is:
320 110 402