GO Program to Implement Deletion of the Last Node from a Linked List

Here we are mainly focused to delete the last node of a linked list. Before that, we should have a basic idea about the linked list. A linked list is a linear data structure in computer programming. All the elements in the linked list are linked using pointers. That means it has one or more nodes and each node containing the data and a pointer to access the next node. Deletion is one of the important linked list operations which helps to delete a node from the linked list. So to delete the last node of a linked list, we should find the second last node and make the next pointer of that node null.

In this program, we will create a single linked list and remove the last node of the given linked list and update the head of the linked list. To accomplish this task, we will find the second last node by traversing through the list. If we found the second last node in the list then update the linked list by deleting the last node by setting its pointer as nill.  Let’s see this program in more detail.

How to delete the last node of Linked List

In this program, we’ll focus our attention on how to delete the last 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. Use another three important functions such as NewNode, TraverseLinkedList, and DeleteLastNode in this program. We can see these functions in more detail.

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 last node from the given linked list, call the function DeleteLastNode(head). This function 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, declare a temporary pointer temp and assign it to the head of the list. It helps you to traverse through the linked list to find the last node. That means we can find the last node by iterating a loop until condition temp.next.next != nil. When "temp.next.next = nill", we can understand temp.next become the second last node position. Delete the last node of the linked list by setting the value of the temp.next to nil. Finally, this function will return the head of the linked list.

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 last node from a given 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 last node from the linked list by calling DeleteLastNode(head).
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 NULL.

Steps to Implement DeleteLastNode(head).

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. Otherwise do step 3
STEP 3: Declare a temporary pointer temp and assign it to the head of the list.
STEP 4: Traverse through the list in order to reach the second last node of the linked list. i.e; iterate the loop with condition temp.next.next != nil. If the loop condition is true then, override temp.next value into temp
STEP 5: Delete the last node of the linked list by setting the value of the temp.next to nil.
STEP 6: Return head
 

GO 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 DeleteLastNode(head *Node) *Node{
   if head == nil{
      return head
   }
   temp := head
   for temp.next.next != nil{
      temp = temp.next
   }
   temp.next = nil
   return head
}
func main(){
   head := NewNode(20, NewNode(40, NewNode(80, NewNode(90, nil))))
   fmt.Printf("Input Linked list is: ")
   TraverseLinkedList(head)
   head = DeleteLastNode(head)
   fmt.Printf("Last node id deleted\nUpdated linked list:")
   TraverseLinkedList(head)
}
                                      

OUTPUT

Input Linked list is: 20 40 80 90 
Last node id deleted
Updated linked list:20 40 80