Problem: Move Matching Values to the End of a Linked List

Write a recursive function:

Item* ll_movtoend(Item* head, int val);

that moves every Item node whose val equals val to the end of the singly-linked list and returns a pointer to the head of the (possibly) updated list.

Requirements and clarifications

You must NOT allocate new Item nodes. You must relink the existing nodes (remove them from their current position and append them to the moved list).

It is not required to preserve the original relative order of the moved nodes; they may appear in any order at the end, but the nodes that are NOT moved must preserve their relative order.

Examples

Node definition

Assume the following Item struct:

struct Item {
    int val;
    Item* next;
    Item(int v, Item* n) : val(v), next(n) {}
};

Skeleton

#include <iostream>

// Item definition
struct Item {
    int val;
    Item* next;
    Item(int v, Item* n) : val(v), next(n) {}
};

// Add a helper function here, if necessary

Item* ll_movtoend(Item* head, int val) 
{

}