Golang Program to Delete the First Node from a Linked List


April 30, 2022, Learn eTutorial
908

In some cases, we want to cancel some nodes from the linked list. Such situations we can handle by using delete operation of linked list. Deletion is one of the important operations of the linked list that allows the deletion of nodes from the linked list.  

We can say a linked list is a linear data structure that includes a bunch of nodes and each node has two attributes such as data and next pointer. 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 first node from the linked list. To accomplish this task, we will iterate the list till the head become nil. If the node is not nil then go to the next node of the head and update the linked list by changing that next node as head. Let’s see this program in more detail.

How to delete the first node from a linked list

In this program, we’ll focus our attention on how to delete the first 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 and fmt.scanln() for reading the input data. We need to import the “fmt” package to use these functions. 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 list 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 first node from the given linked list, call the function DeleteFirstNode(head). It defines a method that accepts the head of a linked list. 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 first node. That means taking the next node of the head as the new head and canceling the old head by assigning nil value to its pointer. Finally, return the new head to the main function. 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 first 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 first node from the linked list by calling DeleteFirstNode(head)
STEP 6: Display the updated linked list 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 starting with the head until we find the reference value as nil.

Steps to Implement DeleteFirstNode(head):

STEP 1: Define a method that accepts the head of a linked list.
STEP 2: Checks, whether the head is equal to nil. If the head is equal to the value nil, return the head without deleting any node. Otherwise do step 3
STEP 3: Take the next node of the head and set it as a new head by deleting the first node.
STEP 4: Return the new head.
 

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 DeleteFirstNode(head *Node) *Node{
   if head == nil{
      return head
   }
   newHead := head.next
   head.next = nil
   return newHead
}
func main(){
   head := NewNode(30, NewNode(10, NewNode(80, NewNode(70, nil))))
   fmt.Printf("Input Linked list is: ")
   TraverseLinkedList(head)
   head = DeleteFirstNode(head)
   fmt.Printf("Updated linked list after deleting first node: ")
   TraverseLinkedList(head)
}

                                      

OUTPUT

Input Linked list is: 30 10 80 70 
Updated linked list after deleting first node: 10 80 70