logo CodeStepByStep logo

linkedListMystery3

Language/Type: C++ linked lists pointers

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

front -> [55] -> [10] -> [2] -> [3] -> [4] -> [20] -> [7] -> [6] -> [8] -> [9] -> [12] -> [15] /

Write 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. Write your answer in this format, without the quotes: "front -> 10 -> 20 -> 30 /".

void linkedListMystery(ListNode*& front) {
    ListNode* curr = front;
    ListNode* next = curr->next;
    while (next != nullptr) {
        if (curr->data % 5 == 0) {
            front = front->next;
        } else if (curr->data % 2 == 0 && next->data % 2 == 0) {
            curr->next = next->next;
        } else if (curr->data % 3 == 0) {
            next->data++;
            curr->data--;
            curr = next;
        }
        curr = next;
        next = next->next;
    }
}

list state

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.