Website Summary Files Commits Branches @commit @diff @stats
path: root / Additional / merge-sorted.cpp
diff options
context:
space:
mode:
1 files changed, 51 insertions, 0 deletions
-rw-r--r--Additional/merge-sorted.cpp51
diff --git a/Additional/merge-sorted.cpp b/Additional/merge-sorted.cpp
new file mode 100644
index 0000000..b801a72
--- /dev/null
+++ b/Additional/merge-sorted.cpp
@@ -0,0 +1,51 @@
+#include<iostream>
+#include"../Seminar04/linked-list-utils.h"
+
+using ListNode = LinkedListNode<int>;
+
+ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
+ if(list1 == nullptr)
+ return list2;
+
+ if(list2 == nullptr)
+ return list1;
+
+ ListNode* start = nullptr;
+ ListNode* iter = nullptr;
+
+ while(list1 != nullptr && list2 != nullptr) {
+ ListNode*& toInsert = (list1->data > list2->data) ? list2 : list1;
+
+ if(iter == nullptr) {
+ start = toInsert;
+ iter = toInsert;
+ }
+ else {
+ iter->next = toInsert;
+ iter = iter->next;
+ }
+
+ toInsert = toInsert->next;
+ }
+
+ ListNode* rest = (list1 == nullptr) ? list2 : list1;
+
+ while(rest != nullptr) {
+ iter->next = rest;
+ iter = iter->next;
+ rest = rest->next;
+ }
+
+ iter->next = nullptr;
+ return start;
+}
+
+int main() {
+ ListNode* l1 = new ListNode(1, new ListNode(2, new ListNode(4)));
+ ListNode* l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
+
+ ListNode* r = mergeTwoLists(l1, l2);
+
+ printLinkedList(r);
+ freeList(r);
+} \ No newline at end of file