- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Merge Two Sorted Linked Lists
In this article, we will write Go language programs to merge two sorted linked lists. A linked list is a set of two fields with the value that is the data and the next pointer which points to the next node in the list.
A linked list is a dynamic data structure with two pointers head and tail, where head points to the first value and tail points to the last value. Here, we will use two examples to merge the sorted linked list.
Demonstration
This demonstration represents two sorted linked lists “LIST1” and “LIST2”. we need to merge these two linked lists into one.
List 1: 1, 2,4 List 2: 1, 3, 4 After merge: 1 1 2 3 4 4
Algorithm
Step 1 − Create a ListNode struct with two field values i.e. the value of the node and the next pointer which is pointing to the next node
Step 2 − Create a function named mergeTwoLists with two input parameters the list1 and the list2 as pointers pointing to the ListNode struct
Step 3 − In this step, create a dummy node that will be used as the head of the new list and a tail node to traverse the list
Step 4 − Then, use a for loop to check if both the lists are not null then check if the value of the list one is less than list two then set tail.next to l1 and l1 to l1.next
Step 5 − If the condition is not satisfied, then for the list2 set tail.next to l2 and l2 to l2.next
Step 6 − Set tail to tail.next and if there are any remaining elements in the list set the next tail to l1 or l2. Finally, return the head of the merged list to the function
Step 7 − In the main, create two lists l1 and l2 with desired values and pass them as arguments in the function, the output will be received by the merged
Step 8 − Run a loop on merged and print the values of the new list in every iteration. The print statement is executed using printf from fmt package.
Example 1
In this example, a dummy will be created to point to the head and tail will be created to traverse the lists and merge them with the help of next pointer and value field. The merged list will be printed using a for loop.
package main import "fmt" type ListNode struct { value int next *ListNode } func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { dummy := &ListNode{} tail := dummy for l1 != nil && l2 != nil { if l1.value < l2.value { tail.next = l1 l1 = l1.next } else { tail.next = l2 l2 = l2.next } tail = tail.next } if l1 != nil { tail.next = l1 } else { tail.next = l2 } return dummy.next } func main() { l1 := &ListNode{value: 1} l1.next = &ListNode{value: 2} l1.next.next = &ListNode{value: 4} l2 := &ListNode{value: 1} l2.next = &ListNode{value: 3} l2.next.next = &ListNode{value: 4} merged := mergeTwoLists(l1, l2) fmt.Println("The merged list l1 and l2 is:") for merged != nil { fmt.Printf("%d ", merged.value) merged = merged.next } }
Output
The merged list l1 and l2 is: 1 1 2 3 4 4
Example 2
In this example, the lists will be merged using recursive approach on the first and second sorted list. The output will be printed using a for loop on the returned list.
package main import "fmt" type ListNode struct { value int next *ListNode } func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { if l1 == nil { return l2 } if l2 == nil { return l1 } if l1.value < l2.value { l1.next = mergeTwoLists(l1.next, l2) return l1 } else { l2.next = mergeTwoLists(l1, l2.next) return l2 } } func main() { l1 := &ListNode{value: 1} l1.next = &ListNode{value: 2} l1.next.next = &ListNode{value: 4} l2 := &ListNode{value: 1} l2.next = &ListNode{value: 3} l2.next.next = &ListNode{value: 4} merged := mergeTwoLists(l1, l2) fmt.Println("The new merged list is:") for merged != nil { fmt.Printf("%d ", merged.value) merged = merged.next } }
Output
The new merged list is: 1 1 2 3 4 4
Conclusion
In this article we have looked how we can execute the program of merging two sorted linked lists using two examples. In the first example, we traverse the list using tail node and merged the nodes whereas in the second example, we used recursion to get the two lists merged.