
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Merge two sorted linked lists using C++.
Problem statement
Given 2 sorted singly linked list. Write a function to merge given two sorted linked lists
List1: 10->15->17->20 List2: 5->9->13->19 Result: 5->9->10->13->15->17->19->20
Algorithm
1. Traverse both lists 1.1. If list1->data < list2->data 1.1.1 Add list1->data to new list and increment list1 pointer 1.2 If list2->data < list1->data 1.2.1 Add list2->data to new list and increment list2 pointer 2. Repeat procedure until both lists are exhausted 3. Return resultant list
Example
#include <iostream> #include <new> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct node { int data; struct node *next; }; node *createList(int *arr, int n){ node *head, *p; p = head = new node; head->data = arr[0]; head->next = NULL; for (int i = 1; i < n; ++i) { p->next = new node; p = p->next; p->data = arr[i]; p->next = NULL; } return head; } void displayList(node *head){ while (head != NULL) { cout << head->data << " "; head = head->next; } cout << endl; } node *mergeSortedLists(node *head1, node *head2){ node *result = NULL; if (head1 == NULL) { return head2; } if (head2 == NULL) { return head1; } if (head1->data < head2->data) { result = head1; result->next = mergeSortedLists(head1->next, head2); } else { result = head2; result->next = mergeSortedLists(head1, head2->next); } return result; } int main(){ int arr1[] = {10, 15, 17, 20}; int arr2[] = {5, 9, 13, 19}; node *head1, *head2, *result = NULL; head1 = createList(arr1, SIZE(arr1)); head2 = createList(arr2, SIZE(arr1)); cout << "First sorted list: " << endl; displayList(head1); cout << "Second sorted list: " << endl; displayList(head2); result = mergeSortedLists(head1, head2); cout << "Final sorted list: " << endl; displayList(result); return 0; }
Output
When you compile and execute the above program. It generates the following output −
First sorted list: 10 15 17 20 Second sorted list: 5 9 13 19 Final sorted list: 5 9 10 13 15 17 19 20
- Related Articles
- Merge K sorted linked lists in Java
- Merge Sort for Linked Lists using C++.
- Merge Two Sorted Lists in Python
- Merge two sorted arrays using C++.
- Merge k Sorted Lists in Python
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- Merge two sorted arrays into a list using C#
- Merge two sorted arrays in C#
- Program to merge K-sorted lists in Python
- Merge two sorted arrays in Python using heapq?
- Intersection of Two Linked Lists in C++
- Program to merge in between linked lists in Python
- Java Program to Merge two lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
- C# program to merge two sorted arrays into one

Advertisements