본문 바로가기
Programming/Data Structure

[Hackerrank] GetNodeValue

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

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);
    }
}

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

[Hackerrank] InsertNodeAtPosition  (0) 2020.12.02
[Hackerrank] InsertNodeAtHead  (0) 2020.12.02
[Hackerrank] FindMergePointofTwoLists  (0) 2020.12.02
[Hackerrank] DeleteNode  (0) 2020.12.02
[Hackerrank] CompareTwoLinkedLists  (0) 2020.12.02