**Get Node Value**

### Problem Statement :

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the tail. The value 2 is at the desired position. Complete the getNode function in the editor below. getNode has the following parameters: SinglyLinkedListNode pointer head: refers to the head of the list int positionFromTail: the item to retrieve Returns int: the value at the desired position Input Format The first line contains an integer t, the number of test cases. Each test case has the following format: The first line contains an integer n, the number of elements in the linked list. The next n lines contains an integer, the data value for an element of the linked list. The last line contains an integer positionFromTail, the position from the tail to retrieve the value of. Function Description

### Solution :

` ````
Solution in C :
In C++ :
/*
Get Nth element from the end in a linked list of integers
Number of elements in the list will always be greater than N.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int GetNode(Node *head,int positionFromTail)
{
// This is a "method-only" submission.
// You only need to complete this method.
int count = 0;
Node *ptr = head;
while(ptr!=NULL)
{
++count;
ptr=ptr->next;
}
count = count-positionFromTail;
ptr=head;
--count;
while(count!=0)
{
--count;
ptr=ptr->next;
}
return ptr->data;
}
In python3 :
def GetNode(head, position):
nums = []
current = head
# First find tail of list as length is unknown
while current != None:
nums.append(current.data)
current = current.next
# Return value of item at given position
return nums[ len( nums ) - position - 1 ]
In java :
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
int GetNode(Node head,int n) {
// This is a "method-only" submission.
// You only need to complete this method.
if(head.next==null)
return head.data;
Node temp=head;
int count=0;
while(temp!=null)
{
count++;
temp=temp.next;
}
int k=count-n-1;
temp=head;
while(k>0)
{
k--;
temp=temp.next;
}
return temp.data;
}
In C :
// Complete the getNode function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
int getNode(SinglyLinkedListNode* head, int positionFromTail) {
int c = 1, i = 1;
SinglyLinkedListNode *ptr = head;
while(ptr->next != NULL){
c += 1;
ptr = ptr->next;
}
if(positionFromTail == 0)
return ptr->data;
ptr = head;
c = c - positionFromTail;
while(i != c){
ptr = ptr->next;
i += 1;
}
return ptr->data;
}
```

## View More Similar Problems

## Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →## Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →## Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →## Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →