From 18cf68855f0008c9388e1ffc87a7699ce28f37e5 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Thu, 13 Feb 2025 23:45:54 +0800 Subject: [PATCH] feat: 21_merge_two_sorted_lists --- 21_merge_two_sorted_lists/main.cpp | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 21_merge_two_sorted_lists/main.cpp diff --git a/21_merge_two_sorted_lists/main.cpp b/21_merge_two_sorted_lists/main.cpp new file mode 100644 index 0000000..5421846 --- /dev/null +++ b/21_merge_two_sorted_lists/main.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { + public: + ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) { + if (!list1 && !list2) { + return nullptr; + } + + ListNode *result = new ListNode{0, nullptr}; + ListNode *currentNode = result; + ListNode *lastNode = nullptr; + + ListNode *current1 = list1; + ListNode *current2 = list2; + + while (current1 != nullptr || current2 != nullptr) { + int val1 = current1 != nullptr ? current1->val : INT32_MAX; + int val2 = current2 != nullptr ? current2->val : INT32_MAX; + + if (val1 <= val2) { + currentNode->val = val1; + current1 = current1->next; + } else { + currentNode->val = val2; + current2 = current2->next; + } + + currentNode->next = new ListNode{0, nullptr}; + lastNode = currentNode; + currentNode = currentNode->next; + } + + lastNode->next = nullptr; + delete currentNode; + + return result; + } +};