logo CodeStepByStep logo

linkedListMystery4

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 -> 25 -> 40 -> 50 -> 20 -> 50 -> 10 -> 8 -> 60 -> 60 -> 37 /

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;
    while (curr->next != nullptr) {
        ListNode* temp = curr->next;
        if (curr->data >= curr->next->data) {
            curr->next = temp->next;
            if (curr->data == temp->data) {
                curr->next = temp->next;
                delete temp;
            } else {
                temp->next = front;
                front = temp;
            }
        } else {
            curr = curr->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.