I am practicing my data structures and trying out this problem on HackerRank on printing the element of a linked list in reverse order.
I have looked through my solutions multiple times but I don't understand why it is wrong.
My Solution:
static void reversePrint(SinglyLinkedListNode head) {
if (head==null){
return;
}
ArrayList<Integer> intList = new ArrayList<>();
while(head!=null){
intList.add(head.data);
head = head.next;
}
Collections.sort(intList, Collections.reverseOrder());
for(int i: intList){
System.out.println(i);
}
}
Appreciate if someone can assist on pointing out my mistakes.