logo CodeStepByStep logo

linkedListMystery2

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

Consider the following linked list of ListNode objects, with a pointer named front that points to the first node:

front -> [1] -> [2] -> [4] -> [3] -> [5] -> [7] -> [11] -> [0] -> [6] -> [1] -> [1] /

Draw the state of the linked list after the following code runs on it. If a given node is removed from the list, you don't need to draw that node, only the ones that remain reachable in the original list.

void linkedListMystery2(ListNode*& front) {
    ListNode* curr = front;
    ListNode* prev = nullptr;
    while (curr->next != nullptr) {
        if (curr->data % 2 == 0 && prev != nullptr) {
            prev->next = curr->next;
        } else {
            curr->data--;
        }
        prev = curr;
        curr = curr->next;
    }
}
list state

You must log in before you can solve this problem.


Log In

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

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.