Golang program to reverse a circular linked list


In this article, we will learn how to create a golang program to reverse a circular linked list using recursion and iterative method. A circular linked list is a type of linked list where the last node in the list points back to the first node, forming a loop. It could be used to implement a circular buffer or a round-robin scheduling algorithm.

Syntax

func reverseList(head **Node){…}

The reverseList() function is used to reverse a circular linked list. It takes an argument as value to the pointer to the address of the head node.

func reverseList(current, prev, head *Node) *Node {…}

The reverseList() function is used to reverse a circular linked list. It takes the arguments as the first non-head node as the current node, nil as the previous node and the head node of the list.

Method 1

In this method, we will define a reverseList() function using iterative method that is used to reverse a circular linked list.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, initialize a node struct to represent the elements of the circular linked list.Each node has a value and a next pointer to the next node.

  • Step 3 − Now, create a nodeAdd() function that adds new node to the circular linked list. It creates a new node with the given value and inserts it after the head node.

  • Step 4 − Define the printLinkedList() function to print the values of all nodes in the circular linked list.

  • Step 5 − Now, create a function called reverseList(). It is used to reverse the order of nodes in the circular linked list.

  • Step 6 − This function takes a pointer to the current head of the list, iterates over the nodes in the list, and swaps their next pointers to reverse the order.

  • Step 7 − Start the main() function. Inside the main() function, insert several nodes into the Circular Linked List.

  • Step 8 − Now, call the reverseList() function and pass the value of the head node pointer as argument to the function.

  • Step 9 − Further, the reversed circular linked list is printed on the screen by using the fmt.Println() function.

Example

Following is the go language program to reverse a circular linked list by using iterative method

package main

import "fmt"

type Node struct {
   value int
   next  *Node
}

func nodeAdd(head **Node, value int) {
   newNode := &Node{value, nil}

   if *head == nil {
      *head = newNode
      newNode.next = *head
   } else {
      newNode.next = (*head).next
      (*head).next = newNode
      *head = newNode
   }
}

func printLinkedList(head *Node) {
   if head == nil {
      fmt.Println("List is empty!")
      return
   }

   fmt.Print(head.value, " ->")
   for current := head.next; current != head; current = current.next {
      fmt.Print(current.value, " ->")
   }
   fmt.Println(head.value)
}

func reverseList(head **Node) {
   if *head == nil {
      return
   }

   current := (*head).next
   prev := *head
   for current != *head {
      next := current.next
      current.next = prev
      prev = current
      current = next
   }

   (*head).next = prev
   *head = prev
}

func main() {
   var head *Node

   nodeAdd(&head, 5)
   nodeAdd(&head, 3)
   nodeAdd(&head, 6)
   nodeAdd(&head, 2)
   nodeAdd(&head, 4)

   fmt.Println("Initial linked list:")
   printLinkedList(head)

   reverseList(&head)

   fmt.Println("Reversed linked list:")
   printLinkedList(head)
}

Output

Initial linked list:
4 -> 5 -> 3 -> 6 -> 2 -> 4
Reversed linked list:
2 -> 6 -> 3 -> 5 -> 4 -> 2

Method 2

In this example, we will define a reverseList() function using recursive method that is used to reverse a circular linked list.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, initialize a node struct to represent the elements of the circular linked list.Each node has a value and a next pointer to the next node.

  • Step 3 − Now, create a nodeAdd() function that adds new node to the circular linked list. It creates a new node with the given value and inserts it after the head node.

  • Step 4 − Define the printList() function to print the values of all nodes in the circular linked list.

  • Step 5 − Now, create a function called reverseList(). It uses a recursive helper function to traverse and reverse the list.

  • Step 6 − If the current node is equal to the head node, next pointer is updated to point the new node of the list.

  • Step 7 − Otherwise, recursively call the function with the next node as the current node, the current node as the previous node and the head node as the same.

  • Step 8 − In each recursive call, update the next pointer of the current node to point to the previous node.

  • Step 9 − Finally, the initial head node is returned as the new node of the list, as it is the last node to be visited and updated.

  • Step 10 − Start the main() function. Inside the main() function, insert several nodes into the Circular Linked List.

  • Step 11 − Now, call the reverseList() function and pass the first non-head node as the current node, nil as the previous node and the head node of the list as arguments to the function.

  • Step 12 − Further, the reversed circular linked list is printed on the screen by using the fmt.Println() function.

Example

Following is the go language program to reverse a circular linked list by using recursive method

package main

import "fmt"

type Node struct {
   value int
   next  *Node
}

func nodeAdd(head *Node, value int) *Node {
   newNode := &Node{value: value}
   if head == nil {
      newNode.next = newNode
   } else {
      newNode.next = head.next
      head.next = newNode
   }
   return newNode
}

func printList(head *Node) {
   if head == nil {
      fmt.Println("Empty list")
      return
   }
   fmt.Print(head.value)
   current := head.next
   for current != head {
      fmt.Printf(" -> %d", current.value)
      current = current.next
   }
   fmt.Printf(" -> %d\n", head.value)
}

func reverseList(current, prev, head *Node) *Node {
   if current == head {
      current.next = prev
      head.next = prev
      return current
   }
   next := current.next
   current.next = prev
   return reverseList(next, current, head)
}

func main() {
   var head *Node
   head = nodeAdd(head, 5)
   head = nodeAdd(head, 3)
   head = nodeAdd(head, 2)
   head = nodeAdd(head, 4)
   head = nodeAdd(head, 1)

   fmt.Println("Initial linked list:")
   printList(head)
   fmt.Println("Reversed linked list:")
   reversed := reverseList(head.next, nil, head)
   printList(reversed)
}

Output

Initial linked list:
1 -> 5 -> 3 -> 2 -> 4 -> 1
Reversed linked list:
1 -> 4 -> 2 -> 3 -> 5

Conclusion

We have successfully compiled and executed a go language program to reverse a circular linked list using recursion and iterative methodalong with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method. The resultant reversed circular linked list is printed to the console as output.

Updated on: 14-Jul-2023

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements