logo CodeStepByStep logo

linkedListMystery11

Language/Type: C++ linked lists pointers
Author: Marty Stepp (on 2018/12/16)

Consider the following two linked lists of ListNode objects, with pointers named front1 and front2 that point to the first node of each list:

front1 ->  2 ->  7 ->  5 -> 33 /
front2 ->  1 -> 10 ->  9 ->  8 -> 66 -> 55 /

Write the final state of the two linked lists after the following code runs. If a given node is removed from either list and not in either list at the end, you don't need to draw that node, only the ones that remain reachable in the original lists. Write your answer in this format, without the quotes: "front1 -> 10 -> 20 -> 30 /".

void linkedListMystery11(ListNode*& front1, ListNode*& front2) {
    ListNode* curr1 = front1;
    while (front2 != nullptr) {
        if (curr1->next == nullptr) {
            curr1->next = front2;
            front2 = nullptr;
        } else if (front2->data < curr1->next->data) {
            ListNode* temp2 = front2;
            front2 = front2->next;
            temp2->next = curr1->next;
            curr1->next = temp2;
        } else {
            curr1 = curr1->next;
        }
    }
}
list 1
list 2

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.