public class Node {
...
public Node getNext() {...} // return next field
public int getData() {...} // returns data
}
Assuming that the variable head points to (i.e. contains the address of) the first node of the linked list, write the statement(s) to print the data value in every other node of the linked list to the console. For example if the list has 5->4->3->2->1, the output should be 5 3 1 i.e. the numbers only with spaces in-between. If the list is empty 0 size), you code should not print out anything.
You may declare additional variables and assume that the list may have any number of nodes including zero. If the list is empty, do not print anything, otherwise print the data separated by white spaces.
What i tried:
if (head == null) {
break;
}
else {
while (head != null) {
int current = head.getData();
System.out.print(current + " ");
head = head.getNext();
if (head == null) {
break;
}
head = head.getNext();
}
}