본문 바로가기
Programming/Data Structure

[Hackerrank] ReversePrint

by 읽고 쓰는 개발자 2020. 12. 2.

https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list-in-reverse/problem

 

Print in Reverse | HackerRank

Print the elements of a linked list in reverse order, from tail to head

www.hackerrank.com

package exam.complete;

import exam.SinglyLinkedListNode;

import java.util.ArrayList;
import java.util.List;
//https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list-in-reverse/problem

public class ReversePrint {
    static void reversePrint(SinglyLinkedListNode head) {
        List<Integer> list = new ArrayList<>();
        while(head != null) {
            list.add(head.data);
            head = head.next;
        }
        for(int i = list.size() -1 ; i >= 0 ; i--) System.out.println(list.get(i));
    }
}

'Programming > Data Structure' 카테고리의 다른 글

[Hackerrank] TreeHeightofaBinaryTree  (0) 2020.12.02
[Hackerrank] SortedInsert  (0) 2020.12.02
[Hackerrank] Reversealinkedlist  (0) 2020.12.02
[Hackerrank] ReverseADoublyLinkedList  (0) 2020.12.02
[Hackerrank] PrintLinkedList  (0) 2020.12.02