Though this is a subjective question, everyone (or most) would agree that the second option is not readable. I missed to notice the ;
at the end.
Make your code readable and easy to understand. Don't make it look smart.
Note: This doesn't mean that you should not use a for loop. Make the for loop equivalent to the while loop.
for (Node ptr = head; ptr.next != null;) {
ptr = ptr.next;
}
Or, at least add an empty body {}
to your for loop code.
for (p = head; (q = p.next) != null; p = q) {
}
I have seen static code analyzer rules set to up fail the compilation/build when loops have an empty body (explicit like above or when using a ;
at the end as in your question).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…