• + 0 comments

    My Java 8 Solution

    public static int getNode(SinglyLinkedListNode llist, int positionFromTail) {
            int lenght = 0;
            SinglyLinkedListNode counterList = llist;
            
            while (counterList != null) {
                lenght++;
                counterList = counterList.next;
            }
            
            int positionFromHead = lenght - positionFromTail - 1;
            int index = 0;
            SinglyLinkedListNode node = llist;
            
            while (index < positionFromHead) {
                node = node.next;
                index++;
            }
            
            return node.data;
        }