Programming/Data Structure

[Hackerrank] GetNodeValue

읽고 쓰는 개발자 2020. 12. 2. 22:29

www.hackerrank.com/challenges/get-the-value-of-the-node-at-a-specific-position-from-the-tail/problem

Get Node Value | HackerRank

Given the head of a linked list, get the value of the node at a given position when counting backwards from the tail.

www.hackerrank.com

package exam.complete;

//https://www.hackerrank.com/challenges/get-the-value-of-the-node-at-a-specific-position-from-the-tail/problem

import exam.SinglyLinkedListNode;

import java.util.ArrayList;
import java.util.List;

public class GetNodeValue {

    static int getNode(SinglyLinkedListNode head, int positionFromTail) {
        List<Integer> list = new ArrayList<>();

        while (head != null) {
            list.add(head.data);
            head = head.next;
        }

        return list.get(list.size() - positionFromTail - 1);
    }
}