I'm stuck with this problem I've been working on for the past few days. The only portion of the program I'm having trouble with is the method which removes nodes from a list. My initial thinking was if the node was to be removed, then I could set the next node to the current node's position; however, when I do this, I get caught in a never ending loop. Any guidance is greatly appreciated.

Code below:

Java Code:
    private static Node removeNodes(Node h, int iVal) {

        while (h != null) { // check if first node is empty
            int temp = h.data;

            if (temp != iVal) { // test to see if temp is equivalent to iVal
                h = h.next; // move to next node
            }

            else {
                h.next = h; // set next node to current node, deleting old node
                h = h.next; // move to next node
            }
        }
        
        return (h);
    }