The line int ans = tmp->next;
appears to be the source of the problem. This is attempting to take the next
pointer in the node, convert it to an int
, and return it. What you (almost certainly) want is to retrieve the data from the node and return that, with something like int ans = tmp->num;
.
Of course, that's not saying the code is perfect otherwise (e.g., it seems to lack any attempt at checking for, not to mention dealing with, errors), but at least with that change, it stands some chance of working correctly under some (ideal) circumstances.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…