-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_list.h
More file actions
79 lines (65 loc) · 2.58 KB
/
Copy pathmerge_list.h
File metadata and controls
79 lines (65 loc) · 2.58 KB
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
#ifndef CPP_ALGORITHM_MERGE_LIST_H
#define CPP_ALGORITHM_MERGE_LIST_H
#include "linked_list.h"
#include <vector>
namespace MergeList
{
/**
* \brief Merge two sorted linked lists.
* \details The task is to merge two sorted linked lists L1 and L2 into one sorted linked list (in place)
* and return the head of the merged list. In worst-case, this task has O(n + m) time complexity,
* where n and m are the length of the lists.
* \param list1 the head of the first list
* \param list2 the head of the second list
* \return the head of merged sorted linked list
*/
std::shared_ptr<LinkedList::Node<int>> MergeTwoSortedLinkedList(
const std::shared_ptr<LinkedList::Node<int>>& list1,
const std::shared_ptr<LinkedList::Node<int>>& list2);
/**
* \brief Merge even and odd nodes in a singly linked list.
* \param list the head of the list
* \return the head of the even-odd merged list
*/
std::shared_ptr<LinkedList::Node<int>> MergeEvenOddLinkedList(
const std::shared_ptr<LinkedList::Node<int>>& list);
}
// ----------------------------------------------------------------------------
inline std::shared_ptr<LinkedList::Node<int>> MergeList::MergeTwoSortedLinkedList(
const std::shared_ptr<LinkedList::Node<int>>& list1,
const std::shared_ptr<LinkedList::Node<int>>& list2)
{
const auto dummy_head = std::make_shared<LinkedList::Node<int>>(LinkedList::Node<int>{0, nullptr});
auto tail = dummy_head;
auto iter1 = list1;
auto iter2 = list2;
while (iter1 && iter2)
{
LinkedList::AppendNode(iter1->data < iter2->data ? &iter1 : &iter2, &tail);
}
tail->next = iter1 ? iter1 : iter2;
return dummy_head->next;
}
// ----------------------------------------------------------------------------
inline std::shared_ptr<LinkedList::Node<int>> MergeList::MergeEvenOddLinkedList(
const std::shared_ptr<LinkedList::Node<int>>& list)
{
if (list == nullptr || list->next == nullptr)
{
return list;
}
const auto even_dummy_head = std::make_shared<LinkedList::Node<int>>(LinkedList::Node<int>{0, nullptr});
const auto odd_dummy_head = std::make_shared<LinkedList::Node<int>>(LinkedList::Node<int>{0, nullptr});
auto tails = std::vector{even_dummy_head, odd_dummy_head};
int turn = 0;
for (auto iter = list; iter; iter = iter->next)
{
tails[turn]->next = iter;
tails[turn] = tails[turn]->next;
turn ^= 1;
}
tails[1]->next = nullptr;
tails[0]->next = odd_dummy_head->next;
return even_dummy_head->next;
}
#endif