public class Traverse {
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
printList(head);
}
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data);
curr = curr.next;
}
}
}
The output is:
10,20,30,40,50,
I don't want the last commma (,
).
I am asking this because in a company interview a similar question was asked and logically the traversal is correct but because of the last comma 1 test case failed. I know it's lame but a blunder so the output should be:
10,20,30,40,50