Golang program to join two linked list


In Go, a linked list is a linear data structure that contains a node which further contains two fields one is data and the other one is next. The next contains the address of the next node in continuation. We will execute this program with the help of two methods. In the first method linked list will be used a series of nodes and in the second example different variables are used to execute the program.

Method 1 − Using linked list as series of nodes

A linked list is described by this program as a collection of Nodes, each of which has a Value and a pointer to the following Node in the list. A pointer to the head of a new list that is created by adding the second list to the end of the first is returned by the joinLists function, which requires two linked lists as parameters. For demonstration purposes, the printList function is used to display the list's contents.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a struct of type node with a value of type int and next pointer of type node.

  • Step 3 − In the next step create a main function and in that function create two linked lists which are to be joined.

  • Step 4 − Call the function joinLists from the main with two lists as inputs.

  • Step 5 − Look to see if one of the linked lists is null; if so, return the non-null linked list.

  • Step 6 − Create a pointer that is initialized to point to the top of the first linked list, l1.

  • Step 7 − Use the current pointer to navigate the first linked list until it reaches current.

  • Step 8 − The nil comes after, signifying that the list has come to an end. The head of the second linked list, l2, comes next. Next, return the first linked list's l1 head.

  • Step 9 − The Next pointers can then be used to navigate the joined linked list from head node to tail node.

  • Step 10 − Use printList function to print the merged linked lists and the statement is executed using fmt.Println() function where ln means new line.

Example

In this example we will use linked list as series of nodes.

package main
import "fmt"
type Node struct {
   Value int
   Next  *Node
}

func main() {
   list1 := &Node{Value: 10, Next: &Node{Value: 20, Next: &Node{Value: 30}}}
   list2 := &Node{Value: 40, Next: &Node{Value: 50, Next: &Node{Value: 60}}}
   joinedList := joinLists(list1, list2)
   fmt.Println("The two lists are merged as follows:")
   printList(joinedList)   //print the merged list
}

func joinLists(li1, li2 *Node) *Node {
   if li1 == nil {
      return li2
   } else if li2 == nil {    //check if the list1 is null return list2 and vice-versa
      return li1
   }

   current := li1
   for current.Next != nil {
      current = current.Next 
   }
   current.Next = li2
   return li1
}

func printList(l *Node) {
   for l != nil {
      fmt.Printf("%d ", l.Value)
      l = l.Next
   }
   fmt.Println()
}

Output

The two lists are merged as follows:
10 20 30 40 50 60 

Method 2: Using different types of variables and their types in linked list

This method employs the same fundamental methodology as the preceding example with some variations in the variable names and types. A pointer to the head of a new list that is created by adding the second list to the end of the first is returned by the joinLists function, which requires two linked lists as parameters. For demonstration purposes, the printList function is used to display the list's contents.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a struct of type node with a value of type int and next pointer of type node.

  • Step 3 − In the next step create a main function and, in that function, create two linked lists which are to be joined.

  • Step 4 − Call the function joinLists and determine which of the linked lists is null, and if either is, return the non-null linked list.

  • Step 5 − Create a pointer with the head initialized to the first linked list, li1.

  • Step 6 − Create the pointer li1 to the first linked list's head.

  • Step 7 − Until li1, use the li1 pointer to navigate the first linked list.

  • Step 8 − The null comes next, signifying that the list's end has been reached.

  • Step 9 − Set the li1.next equals to li2.

  • Step 10 − Then, return the top-most linked list head.

  • Step 11 − Then, by following the Next pointers, the joined linked list can be navigated from head node to tail node.

Example

In this example we will use different variable and types to continue the execution.

package main
import "fmt"
type ListNode struct {
   Val  int
   Next *ListNode
}

func main() {
   list1 := &ListNode{Val: 10, Next: &ListNode{Val: 20, Next: &ListNode{Val: 30}}}
   list2 := &ListNode{Val: 40, Next: &ListNode{Val: 50, Next: &ListNode{Val: 60}}}

   joinedList := joinLists(list1, list2)  
   fmt.Println("The two linked lists are joined as follows:")
   printList(joinedList)  //print the lists merged
}

func joinLists(li1, li2 *ListNode) *ListNode {
   if li1 == nil {
      return li2
   } else if li2 == nil { //check if the list1 is null return list2 and vice-verca
      return li1
   }

   head := li1
   for li1.Next != nil {
      li1 = li1.Next
   }
   li1.Next = li2
   return head
}

func printList(l *ListNode) {
   for l != nil {
      fmt.Printf("%d ", l.Val)
      l = l.Next
   }
   fmt.Println()
}

Output

The two linked lists are joined as follows:
10 20 30 40 50 60 

Conclusion

We executed the program of joining two linked lists using two examples. In the first example we used the linked list as series of nodes and in the second example we did similar operation but different variables and types.

Updated on: 22-Feb-2023

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements