Golang Program to Reverse a Given Linked List


December 28, 2022, Learn eTutorial
1659

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 'LL' are connected using pointers.

How to reverse a linked list using recursion in the go program?

For reversing a linked list, We are importing the “fmt” package to include some standard libraries into the GO program. In order to use these library functions, we need to import the “fmt” package. After that Main function starts and inside that, 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.

ALGORITHM

STEP 1: Start
STEP 2: Define the structure of the node
STEP 3: Create a linked list by calling the function NewNode(). Here the head is the start node.
STEP 4: Call the function TraverseLinkedList(head) to display the input LL.
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.
 


For reversing a linked list in GO, we are using the below concepts. We recommend you to learn those for a better understanding

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){
   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