Find n/k th node in Linked list | Practice | GeeksforGeeks

Given a singly linked list and a number k. Write a function to find the (N/k)th element, where N is the number of elements in the list. We need to consider ceil value in case of decimals.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case consists of an integer N. The second line consists of N spaced integers.The last line consists of an integer k.
Output:
Print the data value of (N/k)th node in the Linked List.
User Task:
The task is to complete the function fractional_node() which should find the n/kth node in the linked list and return its data value.
Constraints: 
1 <= T <= 100
1 <= N <= 100
Example:
Input:
2
6
1 2 3 4 5 6
2
5
2 7 9 3 5
3
Output:
3
7
Explanation:
Testcase 1: 6/2th element is the 3rd(1-based indexing) element which is 3.
 
0
neelasatyasai935 days ago
#PYTHON(from mechanical) 
import mathdef fractionalNodes(head,k):       #add code here       temp=head       c=0       r=0       while temp!=None:           c+=1           temp=temp.next       i=math.ceil(c/k)       temp=head       while r!=i-1:           r+=1           temp=temp.next       return  temp
+1
shahabuddinbravo402 weeks ago
int fractional_node(struct Node *head, int k){   float N=0; //why in float???? because float/int = float and int/int=int   Node *ptr=head;   while(ptr!=NULL){       ptr=ptr->next;       N++;   }        int index=1;      while(head!=NULL){       if(index==ceil(N/k)){           return head->data;       }       head=head->next;       index++;   }   }
0
uma052 weeks ago
Java 1 Pass soln
class GfG
{
    public static int nknode(Node head, int k)
    {
       // add your code here
       Node p1=head;
       Node p2=head;
       
       int p1Pos=1;
       int p2Pos=1;
       
       while(p1.next!=null){
           p1=p1.next;
           p1Pos++;
           
           int reqPos=(int)Math.ceil((1.0*p1Pos)/k);
           
           while(p2Pos<reqPos){
               p2=p2.next;
               p2Pos++;
           }
       }
       
       return p2.data;
    }
}
0
bipulharsh1233 weeks ago
bool kthExists(Node *ptr, int k){   while(k--){       if(!ptr)           return false;       ptr = ptr->next;   }   if(!ptr)       return false;   return true;}Node *kthNode(Node *ptr, int k){   while(k--){       ptr = ptr->next;   }   return ptr;}int fractional_node(struct Node *head, int k){   // your code here   Node *kitr=head, *itr=head;   while(kthExists(kitr, k)){       kitr = kthNode(kitr, k);       itr = itr->next;   }   return itr->data;}
0
2016yashpratap1 month ago
class GfG{   public static int nknode(Node head, int k)    {        Node cur = head;       int len = 0;       while(cur.next!=null)       {           len++;           cur=cur.next;       }      int x = (int)Math.ceil(len/(int)k);      if(head == null || k<0)      {          return -1;      }            Node temp = head;      for(int i=0; i!=x;i++)      {          temp = temp.next;      }      int num = temp.data;      return num;      }   }
+1
technophyle11 month ago
Simple and easy Code :
int fractional_node(struct Node *head, int k)
{
    // your code here
    Node* tmp=head;
    int len=0;
    while(tmp!=NULL)
    {
        len++;
        tmp=tmp->next;
    }
    tmp=head;
    if(k==0)return tmp->data;
    tmp=head;
    for(int i=1; i<ceil((float)len/k); i++){
          tmp=tmp->next;
      }
    return tmp->data;
}
0
0niharika22 months ago
int fractional_node(struct Node *head, int k)
{
    Node *trav = head; int n=0;
    while(trav){
        n++;
        trav = trav->next;
    }
    trav = head;
    int i=1, ind=ceil(n/(k+0.0));
    while((i++)<ind){
        trav = trav->next;
    }
    return trav->data;
}
0
mridulbhaskarabc2 months ago
 
import math
def fractionalNodes(head,k):
        #add code here
        node_traverse = head
        node_ref = head
        count = 1
        while(node_traverse.next):
            count+=1
            node_traverse = node_traverse.next
        index = math.ceil(count/k)
        if(index==1):
            return head
        else:
            for i in range(index-1):
                node_ref = node_ref.next
            return node_ref
#Contributed By: Mridul Bhaskar
 
0
chhitizgoyal2 months ago
Java Solution.
  Node temp=head;      int len=0;      while(temp!=null){          len++;          temp=temp.next;      }      temp = head;      int nk = (len+k-1)/k;      for(int i=0; i<nk-1; i++){          temp=temp.next;      }      return temp.data;   }
0
shreshthchaturvedi24013 months ago
C Language Code :
int fractional_node(struct Node *head, int k){   struct Node*ptr=head;   float count=0.0;   while(ptr!=NULL){       count++;       ptr=ptr->next;   }   ptr=head;float x=ceil(count/k);      for(int i=1; i<x; i++){       ptr=ptr->next;   }   return (ptr->data);} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.