
we need to iterate through all nodes and print all value, and the pointer should stop at the last valid node
how to do it without using break statement in while loop?
// this will miss the last node
while (head.next != null) {
System.out.println(head.val);
head = head.next;
}
// the pointer will stop on null
while (head != null) {
System.out.println(head.val);
head = head.next;
}

// "work" but doesnt look nice...
while (head.next != null) {
System.out.println(head.val);
head = head.next;
}
System.out.println(head.val);
// "work" but doesnt look nice...
Node prev;
while (head = null) {
System.out.println(head.val);
prev = head;
head = head.next;
}
System.out.println(head.val);
return prev;



// "work" but doesnt look nice...
while (true) {
System.out.println(head.val);
if (head.next != null) head = head.next;
else break;
}