logo CodeStepByStep logo

linkedListMystery11

Language/Type: C++ linked lists pointers

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?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.