When one list is exhausted the remaining keys come from the other list For

When one list is exhausted the remaining keys come

This preview shows page 3 - 4 out of 4 pages.

* When one list is exhausted, the remaining keys come from the other list. * For example: [1, 2] and [3, 4, 5] would return [1, 3, 2, 4, 5] */ Node* interleave(Node* list1, Node* list2){ // ******** WRITE YOUR CODE HERE ******** Node* head = NULL; Node* curr = head; while(list1 != NULL && list2 != NULL) { Node* n1 = new Node; Node* n2 = new Node; n1->key = list1->key; n2->key = list2->key; n1->next = n2; n2->next = NULL; if(head == NULL) { head = n1; } else { curr->next = n1; } curr = n2; list1 = list1->next; list2 = list2->next; } while(list2 != NULL) { Node* n2 = new Node; n2->key = list2->key; n2->next = NULL; if(head == NULL) { head = n2; curr = head; } else { curr->next = n2; curr = curr->next; } list2 = list2->next; } while(list1 != NULL) {
Image of page 3
Node* n1 = new Node; n1->key = list1->key; n1->next = NULL; if(!head) { head = n1; curr = head; } else { curr->next = n1; curr = curr->next; } list1 = list1->next; } return head; }
Image of page 4

You've reached the end of your free preview.

Want to read all 4 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture