GO Program to Reverse a Given Linked List

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

What is a LINKED LIST?

A linked list is one of the linear data structures that we come across in computer programming. It has a bunch of nodes and each node contains the data and a pointer to access the next node. We can see all the elements in the linked list are linked using pointers.

How to implement reverse a given linked list in GO using recursion

In this section, we’ll focus our attention on how to reverse a given linked list by using GO language.

Here we are importing the “fmt” package to include some standard libraries into the program. In order to use these library functions, we need to import the “fmt” package. After that Main function starts, inside the main, we are doing the whole program. 

This Go program defines a method that accepts the head of a linked list. To display the reversal of the input linked list, call the function ReverseLinkedList(head). This function will check if the head is equal to nil. If so; return otherwise, call ReverseLinkedList, recursively. Finally, display the head node value at the end.

Given below are the steps that are used in the Go program to reverse 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: Call the function TraverseLinkedList(head) to display the input linked list.
STEP 5: Display the reversal of the input linked list by calling the function ReverseLinkedList(head)
STEP 6: Exit

Steps to Implement NewNode(value int)

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 *Node)

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 ReverseLinkedList(head *Node)

STEP 1: Check the condition head == nil, If so, return; Otherwise, call ReverseLinkedList, recursively.
Step  2: Display the value of the head at the end.
 

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){
   fmt.Printf("Input Linked List is: ")
   temp := head
   for temp != nil {
      fmt.Printf("%d ", temp.value)
      temp = temp.next
   }
   fmt.Println()
}
func ReverseLinkedList(head *Node){
   if head == nil{
      return
   }
   ReverseLinkedList(head.next)
   fmt.Printf("%d ", head.value)
}
func main(){
   head := NewNode(80, NewNode(70, NewNode(55, NewNode(90, nil))))
   TraverseLinkedList(head)
   fmt.Printf("Reversal of the input linked list is: ")
   ReverseLinkedList(head)
}
                                      

OUTPUT

Input Linked List is: 80 70 55 90 
Reversal of the input linked list is: 90 55 70 80