|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +class SinglyLinkedListNode { |
| 6 | + public: |
| 7 | + int data; |
| 8 | + SinglyLinkedListNode *next; |
| 9 | + |
| 10 | + SinglyLinkedListNode(int node_data) { |
| 11 | + this->data = node_data; |
| 12 | + this->next = nullptr; |
| 13 | + } |
| 14 | +}; |
| 15 | + |
| 16 | +class SinglyLinkedList { |
| 17 | + public: |
| 18 | + SinglyLinkedListNode *head; |
| 19 | + SinglyLinkedListNode *tail; |
| 20 | + |
| 21 | + SinglyLinkedList() { |
| 22 | + this->head = nullptr; |
| 23 | + this->tail = nullptr; |
| 24 | + } |
| 25 | + |
| 26 | + void insert_node(int node_data) { |
| 27 | + SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); |
| 28 | + |
| 29 | + if (!this->head) { |
| 30 | + this->head = node; |
| 31 | + } else { |
| 32 | + this->tail->next = node; |
| 33 | + } |
| 34 | + |
| 35 | + this->tail = node; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { |
| 40 | + while (node) { |
| 41 | + fout << node->data; |
| 42 | + |
| 43 | + node = node->next; |
| 44 | + |
| 45 | + if (node) { |
| 46 | + fout << sep; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void free_singly_linked_list(SinglyLinkedListNode* node) { |
| 52 | + while (node) { |
| 53 | + SinglyLinkedListNode* temp = node; |
| 54 | + node = node->next; |
| 55 | + |
| 56 | + free(temp); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Complete the mergeLists function below. |
| 61 | + |
| 62 | +/* |
| 63 | + * For your reference: |
| 64 | + * |
| 65 | + * SinglyLinkedListNode { |
| 66 | + * int data; |
| 67 | + * SinglyLinkedListNode* next; |
| 68 | + * }; |
| 69 | + * |
| 70 | + */ |
| 71 | +SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { |
| 72 | + SinglyLinkedListNode* temp; |
| 73 | + |
| 74 | + if(head1==NULL&&head2==NULL) |
| 75 | + return NULL; |
| 76 | + if(head1==NULL) |
| 77 | + return head2; |
| 78 | + if(head2==NULL) |
| 79 | + return head1; |
| 80 | + |
| 81 | + if(head1->data<head2->data) |
| 82 | + { |
| 83 | + head1->next=mergeLists(head1->next,head2); |
| 84 | + return head1; |
| 85 | + } |
| 86 | + |
| 87 | + else |
| 88 | + { |
| 89 | + head2->next=mergeLists(head2->next,head1); |
| 90 | + return head2; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +int main() |
| 95 | +{ |
| 96 | + ofstream fout(getenv("OUTPUT_PATH")); |
| 97 | + |
| 98 | + int tests; |
| 99 | + cin >> tests; |
| 100 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 101 | + |
| 102 | + for (int tests_itr = 0; tests_itr < tests; tests_itr++) { |
| 103 | + SinglyLinkedList* llist1 = new SinglyLinkedList(); |
| 104 | + |
| 105 | + int llist1_count; |
| 106 | + cin >> llist1_count; |
| 107 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 108 | + |
| 109 | + for (int i = 0; i < llist1_count; i++) { |
| 110 | + int llist1_item; |
| 111 | + cin >> llist1_item; |
| 112 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 113 | + |
| 114 | + llist1->insert_node(llist1_item); |
| 115 | + } |
| 116 | + |
| 117 | + SinglyLinkedList* llist2 = new SinglyLinkedList(); |
| 118 | + |
| 119 | + int llist2_count; |
| 120 | + cin >> llist2_count; |
| 121 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 122 | + |
| 123 | + for (int i = 0; i < llist2_count; i++) { |
| 124 | + int llist2_item; |
| 125 | + cin >> llist2_item; |
| 126 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 127 | + |
| 128 | + llist2->insert_node(llist2_item); |
| 129 | + } |
| 130 | + |
| 131 | + SinglyLinkedListNode* llist3 = mergeLists(llist1->head, llist2->head); |
| 132 | + |
| 133 | + print_singly_linked_list(llist3, " ", fout); |
| 134 | + fout << "\n"; |
| 135 | + |
| 136 | + free_singly_linked_list(llist3); |
| 137 | + } |
| 138 | + |
| 139 | + fout.close(); |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
0 commit comments