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
}

return
}

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
}

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

}
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:")
fmt.Println("Updated Reordered list:")
}


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
}

return
}

var prev *Node

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

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

}

}

}

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
}
}

}
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:")

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


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

51 Views