Could anyone explain this code to me？Leetcode #180
But it works in leetcode

Yes. This code is syntactically correct, so it does not produce errors. But it is not deterministic -- two executions of it can return different outputs whereas source data is the same. Forget this code. It is non-deterministic - i.e. it is wrong.
The code is processing order-dependent see online demo.  But ORDER BY clause is absent - so the query is wrong.