Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Merge2Lists(Node* list1, Node* list2, Node* result)
- {
- Node* current = NULL;
- Node* end_of_result = result;
- while (list1 != NULL && list2 != NULL)
- {
- OP += 2;
- if (list1->data <= list2->data)
- {
- // Creating a new node that will store the data of list1
- current = (Node*)malloc(sizeof(Node));
- current->data = list1->data;
- current->next = NULL;
- // Going to the next element
- list1 = list1->next;
- end_of_result->next = current;
- OP += 4;
- }
- else
- {
- // Creating a new node that will store the data of list2
- current = (Node*)malloc(sizeof(Node));
- current->data = list2->data;
- current->next = NULL;
- // Going to the next element
- list2 = list2->next;
- end_of_result->next = current;
- OP += 4;
- }
- end_of_result = end_of_result->next;
- OP += 2;
- }
- if (list1 == NULL || list2 == NULL) OP += 2;
- if (list1 == NULL)
- end_of_result->next = list2;
- else
- end_of_result->next = list1;
- OP++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement