Golang program to reorder list using recursion


A linked list is a data structure consisting of a collection of nodes, where each node contains a value and a pointer to the next node in the list. In this Golang article, we will learn how to reorder list using recursion along with some helper functions.

Syntax

func reorderList(head *Node) {…}

The reorderList() function is used to reorder list using recursion. It takes pointer to the head node as its argument.

Algorithm

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

  • Step 2 − Now, create a struct of a single node of the linked list named, Node. It contains two members, one to hold data value of node and second is the pointer to point to the next node in the list.

  • Step 3 − Now, create a function named reorderList() that takes the head node of the linked list as input and modify the list to reorder it. It uses recursion to implement this.

  • Step 4 − Check if the linked list is empty or it contains only one node, then there is no need to reorder it.

  • Step 5 − Then, find the middle of the linked list using the findMiddle helper function. It uses the two-pointer technique.

  • Step 6 − Next, reverse the second half of the list using the reverselist helper function. It takes the head of the second half of the list and returns the head of the reversed list.

  • Step 7 − Then, merge the first and reversed second halves of the linked list using the mergeLists helper function. The merged list will be the reordered list.

  • Step 8 − Finally, update the head node of the input linked list to the head of the reordered list.

  • Step 9 − Start the main() function. Inside the main() function, add nodes to the linked list.

  • Step 10 − Now, call the reorderList() function to reorder the list and prints the updated list.

  • Step 11 − Further, the resultant updated reordered linked list using recursion is printed on the screen by calling the printList() function .

Example1: Go Language Program To Reorder List Using Recursion

In this example, we will define a reorderList() function using recursion that is used to reorder the singly linked list.

package main

import "fmt"

type Node struct {
   Val  int
   Next *Node
}

func reorderList(head *Node) {
   if head == nil || head.Next == nil || head.Next.Next == nil {
      return
   }

   fast, slow := head, head
   for fast.Next != nil && fast.Next.Next != nil {
      fast = fast.Next.Next
      slow = slow.Next
   }

   secondHalf := slow.Next
   slow.Next = nil
   var prev *Node
   for secondHalf != nil {
      next := secondHalf.Next
      secondHalf.Next = prev
      prev = secondHalf
      secondHalf = next
   }

   p1, p2 := head, prev
   for p2 != nil {
      next1, next2 := p1.Next, p2.Next
      p1.Next = p2
      p2.Next = next1
      p1, p2 = next1, next2
   }
}

func printList(head *Node) {
   for head != nil {
      fmt.Printf("%d ->", head.Val)
      head = head.Next
   }
   fmt.Println("nil")
}

func main() {
   head := &Node{Val: 8, Next: &Node{Val: 7, Next: &Node{Val: 3, Next: &Node{Val: 4, Next: nil}}}}
   fmt.Println("Initial Ordered list:")
   printList(head)
   reorderList(head)
   fmt.Println("Updated Reordered list:")
   printList(head)
}

Output

Initial Ordered list:
8 -> 7 -> 3 -> 4 -> nil
Updated Reordered list:
8 -> 4 -> 7 -> 3 -> nil 

Example 2

In this example, we will define a reorderList() function using recursion that is used to reorder the singly linked list with the help of helper functions.

package main

import "fmt"

type Node struct {
   Val  int
   Next *Node
}

func reorderList(head *Node) {
   if head == nil || head.Next == nil {
      return
   }

   var prev *Node
   slow, fast := head, head

   for fast != nil && fast.Next != nil {
      prev = slow
      slow = slow.Next
      fast = fast.Next.Next
   }

   prev.Next = nil
   secondHalf := reverseList(slow)

   mergeLists(head, secondHalf)
}

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

   newHead := reverseList(head.Next)
   head.Next.Next = head
   head.Next = nil

   return newHead
}

func mergeLists(l1, l2 *Node) {
   for l1 != nil {
      l1Next := l1.Next
      l2Next := l2.Next

      l1.Next = l2
      if l1Next == nil {
         break
      }

      l2.Next = l1Next
      l1 = l1Next
      l2 = l2Next
   }
}

func printList(head *Node) {
   for head != nil {
      fmt.Printf("%d ->", head.Val)
      head = head.Next
   }
   fmt.Println("nil")
}

func main() {
   head := &Node{Val: 5, Next: &Node{Val: 7, Next: &Node{Val: 3, Next: &Node{Val: 2, Next: &Node{Val: 4, Next: nil}}}}}
   fmt.Println("Initial Ordered list:")
   printList(head)

   reorderList(head)

   fmt.Println("Updated Reordered list:")
   printList(head)
}

Output

Initial Ordered list:
5 -> 7 -> 3 -> 2 -> 4 -> nil
Updated Reordered list:
5 -> 4 -> 7 -> 2 -> 3 -> nil

Conclusion

We have successfully compiled and executed a go language program to going to reorder list with some helper functions by using recursion methodalong with two examples. In the first example, we have used the recursive method and in the second example, we have used the recursive method along with helper functions.

Updated on: 10-May-2023

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements