Website Summary Files Commits Branches @commit @diff @stats
path: root / Additional / Additional-2023-SI / MergeIntoList.cpp
diff options
context:
space:
mode:
Commit7eee5a78005a9a569186d2c8b4b645a818ab9bf1 (patch)
Author
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-06 07:04:44 -0400
Committer
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-06 07:04:44 -0400
Files84594e0af29cefa5aae76ace5f8eb81484400e31 /Additional/Additional-2023-SI/MergeIntoList.cpp
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.tar
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.tar.gz
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.zip
Parent0841635826cb03c0e74cd75ec3c97c4c3b594d8b (diff)
Cleanup
1 files changed, 0 insertions, 117 deletions
-rw-r--r--Additional/Additional-2023-SI/MergeIntoList.cpp117
diff --git a/Additional/Additional-2023-SI/MergeIntoList.cpp b/Additional/Additional-2023-SI/MergeIntoList.cpp
deleted file mode 100644
index 7d575aa..0000000
--- a/Additional/Additional-2023-SI/MergeIntoList.cpp
+++ /dev/null
@@ -1,117 +0,0 @@
-#include <iostream>
-
-struct ListNode
-{
- int val;
- ListNode *next;
- ListNode(int x, ListNode *n = nullptr) : val(x), next(n) {}
-};
-
-ListNode *advance(ListNode *from, size_t index)
-{
- while (index)
- {
- if (from == nullptr)
- {
- return nullptr;
- }
-
- from = from->next;
- --index;
- }
-
- return from;
-}
-
-ListNode *getTail(ListNode *list)
-{
- if (!list || !list->next)
- {
- return list;
- }
-
- while (list->next)
- {
- list = list->next;
- }
- return list;
-}
-
-void freeRagne(ListNode *from, ListNode *to)
-{
- while (from != to)
- {
- ListNode *node = from;
- from = from->next;
- delete node;
- }
-}
-
-void free(ListNode *ll)
-{
- while (ll)
- {
- ListNode *toDelete = ll;
- ll = ll->next;
- delete toDelete;
- }
-}
-
-ListNode *addFront(ListNode *destination, ListNode *target, size_t to)
-{
- ListNode *tail = getTail(target);
- ListNode *end = advance(destination, to + 1);
- tail->next = end;
- freeRagne(destination, end);
- return target;
-}
-
-ListNode *mergeInBetween(ListNode *destination, ListNode *target, size_t from, size_t to)
-{
- if (from == 0)
- {
- return addFront(destination, target, to);
- }
-
- ListNode *predBegin = advance(destination, from - 1);
- if (!predBegin)
- {
- return nullptr;
- }
-
- ListNode *postEnd = advance(predBegin, to - from + 2);
- ListNode *toDeleteHolder = predBegin->next;
- ListNode *targetTail = getTail(target);
-
- predBegin->next = target;
- targetTail->next = postEnd;
-
- freeRagne(toDeleteHolder, postEnd);
-
- return destination;
-}
-
-int main()
-{
- ListNode *fst = new ListNode(1,
- new ListNode(2,
- new ListNode(3,
- new ListNode(4,
- new ListNode(6)))));
-
- ListNode *snd = new ListNode(3,
- new ListNode(4,
- new ListNode(5)));
-
- ListNode *ans = mergeInBetween(fst, snd, 1, 3);
-
- ListNode *it = ans;
-
- while(it)
- {
- std::cout << it->val << " ";
- it = it->next;
- }
-
- free(ans);
-} \ No newline at end of file