Website Summary Files Commits Branches @commit @diff @stats
path: root / Additional / merge-sorted.cpp
diff options
context:
space:
mode:
Commit9eed63a37a808c59990feaaaa3e1725dfdd31f3c (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2023-10-30 22:33:14 +0200
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2023-10-30 22:33:14 +0200
Filesbc22e2b8407f9207c7a5a8ef999f41446c0cee19 /Additional/merge-sorted.cpp
Data-structures-and-algorithms-9eed63a37a808c59990feaaaa3e1725dfdd31f3c.tar
Data-structures-and-algorithms-9eed63a37a808c59990feaaaa3e1725dfdd31f3c.tar.gz
Data-structures-and-algorithms-9eed63a37a808c59990feaaaa3e1725dfdd31f3c.zip
Parent0f17a224d2934c748b32b908f74724b3299d263a (diff)
Add SI consultation
1 files changed, 0 insertions, 51 deletions
-rw-r--r--Additional/merge-sorted.cpp51
diff --git a/Additional/merge-sorted.cpp b/Additional/merge-sorted.cpp
deleted file mode 100644
index b801a72..0000000
--- a/Additional/merge-sorted.cpp
+++ /dev/null
@@ -1,51 +0,0 @@
-#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