forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge sorted list.cpp
86 lines (69 loc) · 2.11 KB
/
merge sorted list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(0); // Create a dummy node
ListNode* current = &dummy; // Pointer to build the new list
while (list1 != nullptr && list2 != nullptr) {
if (list1->val <= list2->val) {
current->next = list1; // Attach list1 node
list1 = list1->next;
} else {
current->next = list2; // Attach list2 node
list2 = list2->next;
}
current = current->next; // Move current to the next position
}
// Attach remaining nodes
current->next = (list1 != nullptr) ? list1 : list2;
return dummy.next; // Return the head of the merged list
}
};
// Function to create a linked list from user input
ListNode* createList() {
int n;
cout << "Enter number of nodes in the list: ";
cin >> n;
if (n <= 0) return nullptr; // Return null for empty lists
ListNode* head = nullptr;
ListNode* tail = nullptr;
for (int i = 0; i < n; ++i) {
int value;
cin >> value;
ListNode* newNode = new ListNode(value);
if (!head) {
head = newNode;
tail = head;
} else {
tail->next = newNode;
tail = tail->next;
}
}
return head;
}
// Function to print the linked list
void printList(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
cout << current->val << " ";
current = current->next;
}
cout << std::endl;
}
int main() {
Solution solution;
cout << "Creating first sorted linked list:\n";
ListNode* list1 = createList();
cout << "Creating second sorted linked list:\n";
ListNode* list2 = createList();
ListNode* mergedList = solution.mergeTwoLists(list1, list2);
cout << "Merged sorted linked list:\n";
printList(mergedList);
return 0;
}