If you are allowed to use a different data structure, use the stack.
Step 1: Move the linked list from the head of the node and push the key onto the stack until you reach the last node. It takes O (n) time.
Step 2: pull items from the stack. It will take O (1) time.
Consequently, the code will be
while(head != null){
stack.push(head.val);
head = head.next;
}
while(stack.isEmpty() != true){
stack.pop();
}