I'm currently implementing a linked list and I'm having a bit of trouble on a function that erases an element. Below is the function in its entirety. If the list is empty, I exit the program. If the list only has a single element, then I just use another function pop_back(); which is functional. Lastly, if the element is in the middle of the list, I iterate through the list until I find the link right before it. I then assign the pointer to the next link to the next link of the node to be erased. The function does remove an element, but it runs into a problem.
template <class T>
void List<T>::erase(iterator & pos) {
    Node<T> * currentNode = pos.current;
    Node<T> * nextNode = pos.current->next;
    Node<T> * searchNode = head;
    if (currentNode == 0) {
        cout << "LIST EMPTY. (ERASE)" << endl;
        exit(0);
    }
    else if (nextNode == 0) {
        pop_back();
    }
    else {
        while (searchNode->next != currentNode) {
            searchNode = searchNode->next;
        }
        searchNode->next = nextNode;
        delete currentNode;
        listsize--;
    }
}
I used the test code below to make sure my function works, but it seems to break after removing the element from the list.
int main() {
    List<int> l;
    l.push_front(5);
    l.push_front(3);
    l.push_back(31);
    l.push_back(354);
    l.push_front(63);
    l.displayList();
    for (Listiterator<int>::iterator it = l.begin(); it != l.end(); it++) {
        if (*it == 31) {
            l.erase(it);
            l.displayList();
        }
    }
    l.displayList();
    return 0;
}
Here are the steps that the code goes through:
63 -> 3 -> 5 -> 31 -> 354 -> NULL
(ERASE ELEMENT 31)
63 -> 3 -> 5 -> 354 -> NULL
Through the debugger I was able to see that after deleting 31, the iterator is positioned in a NULL/illegal position in memory and terminates.
Does anyone have any thoughts or suggestions?
Here are the overloaded ++ operators:
template <class T>
Listiterator<T> & Listiterator<T>::operator++() {
    current = current->next;
    return *this;
}
template <class T>
Listiterator<T> & Listiterator<T>::operator++(int) {
    Listiterator<T> saveList = Listiterator(this);
    current = current->next;
    return saveList;
}
 
     
     
    