Find the fractional (or n/k - th) node in linked list
Last Updated : 13 Jun, 2026
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.
Examples:
Input: 1->2->3->4->5->6 , k = 2 Output: 3 Explanation: 6/2th element is the 3rd(1-based indexing) element which is 3.
Input: 2->7->9->3->5 , k = 3 Output: 7 Explanation: The 5/3rd element is the 2nd element as mentioned in the question that we need to consider ceil value in the case of decimals. So 2nd element is 7.
[Naive Approach] Using Two Traversals - O(n) Time O(n) Space
The approach counts the total number of nodes in the linked list and then calculates the position of the fractional node, which is the (c / k) + 1-th node, where c is the total number of nodes and k is the given interval. Afterward, it traverses the list again to find the node at that position and returns its data. If no fractional node is found, it returns -1.
Traverse the linked list once to count the total number of nodes c.
Compute the fractional position d = ceil(c / k) using (c / k) or (c / k) + 1.
Reset the pointer to the head and start traversing again.
Move node by node until reaching the d-th position.
Return the data of that node (or -1 if not found).
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};/* Function to find the fractional node in the linked list */intfractionalNode(Node*head,intk){intc=0,d;Node*curr=head;// Counting total nodeswhile(curr!=nullptr){c++;curr=curr->next;}// Calculating the distanceif(c%k==0)d=c/k;elsed=(c/k)+1;intp=1;curr=head;// Looping through the linked list// to find the fractional nodewhile(p<=c){// Returning the data of the// node at position dif(p==d){returncurr->data;}p++;curr=curr->next;}return-1;}intmain(){Node*head=newNode(2);head->next=newNode(7);head->next->next=newNode(9);head->next->next->next=newNode(3);head->next->next->next->next=newNode(5);intk=3;cout<<fractionalNode(head,k);return0;}
Java
// Importing necessary librariespublicclassGfG{staticclassNode{intdata;Nodenext;Node(intx){data=x;next=null;}}/* Function to find the fractional node in the linked list */staticintfractionalNode(Nodehead,intk){intc=0,d;Nodecurr=head;// Counting total nodeswhile(curr!=null){c++;curr=curr.next;}// Calculating the distanceif(c%k==0)d=c/k;elsed=(c/k)+1;intp=1;curr=head;// Looping through the linked list// to find the fractional nodewhile(p<=c){// Returning the data of the// node at position dif(p==d){returncurr.data;}p++;curr=curr.next;}return-1;// In case no fractional node is found}publicstaticvoidmain(String[]args){Nodehead=newNode(2);head.next=newNode(7);head.next.next=newNode(9);head.next.next.next=newNode(3);head.next.next.next.next=newNode(5);intk=3;System.out.println(fractionalNode(head,k));}}
Python
# Definition for a nodeclassNode:def__init__(self,x):self.data=xself.next=None# Function to find the fractional node # in the linked listdeffractionalNode(head,k):c=0curr=head# Counting total nodeswhilecurrisnotNone:c+=1curr=curr.next# Calculating the distanced=c//kifc%k==0else(c//k)+1p=1curr=head# Looping through the linked list # to find the fractional nodewhilep<=c:# Returning the data of the# node at position difp==d:returncurr.datap+=1curr=curr.nextreturn-1# In case no fractional node is foundif__name__=='__main__':head=Node(2)head.next=Node(7)head.next.next=Node(9)head.next.next.next=Node(3)head.next.next.next.next=Node(5)k=3print(fractionalNode(head,k))
C#
usingSystem;// Definition for a nodepublicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}/* Function to find the fractional node in the linked list */publicclassGfG{publicstaticintFractionalNode(Nodehead,intk){intc=0,d;Nodecurr=head;// Counting total nodeswhile(curr!=null){c++;curr=curr.next;}// Calculating the distanced=(c%k==0)?c/k:(c/k)+1;intp=1;curr=head;// Looping through the linked list// to find the fractional nodewhile(p<=c){// Returning the data of the// node at position dif(p==d){returncurr.data;}p++;curr=curr.next;}return-1;// In case no fractional node is found}publicstaticvoidMain(){Nodehead=newNode(2);head.next=newNode(7);head.next.next=newNode(9);head.next.next.next=newNode(3);head.next.next.next.next=newNode(5);intk=3;Console.WriteLine(FractionalNode(head,k));}}
JavaScript
// Definition for a nodeclassNode{constructor(x){this.data=x;this.next=null;}}/* Function to find the fractional node in the linked list */functionfractionalNode(head,k){letc=0;letcurr=head;// Counting total nodeswhile(curr!==null){c++;curr=curr.next;}// Calculating the distanceletd=(c%k===0)?c/k:Math.floor(c/k)+1;letp=1;curr=head;// Looping through the linked list // to find the fractional nodewhile(p<=c){// Returning the data of the// node at position dif(p===d){returncurr.data;}p++;curr=curr.next;}return-1;// In case no fractional node is found}// Example usagelethead=newNode(2);head.next=newNode(7);head.next.next=newNode(9);head.next.next.next=newNode(3);head.next.next.next.next=newNode(5);letk=3;console.log(fractionalNode(head,k));
Output
7
Time Complexity: O(n) Auxiliary Space: O(n)(due to the linked list storage).
[Efficient Approach] Using One Traversal - O(n) Time O(1) Space
We can optimize the above solution to work in single traversal. The idea is based on the fact that we need to move the result node after every k. So we traverse the list and whenever the current position becomes multiple of k, we move the result to the next of its current value.
Traverse the linked list once and keep counting nodes using n.
Every time n % k == 0, move the pointer res one step ahead (initialize it at head first time).
Continue this process till the end of the list in a single traversal.
After traversal, if n % k != 0, move res one extra step to handle ceiling value.
Return the data of res as the fractional node.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};intfractionalNode(Node*head,intk){if(head==nullptr||k<=0)return-1;Node*res=nullptr;intn=0;Node*curr=head;// Single traversalwhile(curr!=nullptr){n++;// Every time the node number is divisible by k,// move res by oneif(n%k==0){if(res==nullptr)res=head;elseres=res->next;}curr=curr->next;}// If n % k is not zero, then we need to take // ceil value of n / k if(n%k!=0)res=res->next;returnres->data;}intmain(){Node*head=newNode(2);head->next=newNode(7);head->next->next=newNode(9);head->next->next->next=newNode(3);head->next->next->next->next=newNode(5);intk=3;cout<<fractionalNode(head,k)<<endl;return0;}
Java
// Java program to find fractional node in linked listclassNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{staticintfractionalNode(Nodehead,intk){if(head==null||k<=0)return-1;Noderes=null;intn=0;Nodecurr=head;// Single traversalwhile(curr!=null){n++;// Every time the node number is divisible by k,// move res by oneif(n%k==0){if(res==null)res=head;elseres=res.next;}curr=curr.next;}// If n % k is not zero, then we need to take // ceil value of n / k if(n%k!=0)res=res.next;returnres.data;}publicstaticvoidmain(String[]args){Nodehead=newNode(2);head.next=newNode(7);head.next.next=newNode(9);head.next.next.next=newNode(3);head.next.next.next.next=newNode(5);intk=3;System.out.println(fractionalNode(head,k));}}
Python
# Python program to find fractional node in linked listclassNode:def__init__(self,x):self.data=xself.next=NonedeffractionalNode(head,k):ifheadisNoneork<=0:return-1res=Nonen=0curr=head# Single traversalwhilecurrisnotNone:n+=1# Every time the node number is divisible by k,# move res by oneifn%k==0:ifresisNone:res=headelse:res=res.nextcurr=curr.next# If n % k is not zero, then we need to take # ceil value of n / k ifn%k!=0:res=res.nextreturnres.dataif__name__=="__main__":head=Node(2)head.next=Node(7)head.next.next=Node(9)head.next.next.next=Node(3)head.next.next.next.next=Node(5)k=3print(fractionalNode(head,k))
C#
// C# program to find fractional node in linked listusingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGfG{staticintFractionalNode(Nodehead,intk){if(head==null||k<=0)return-1;Noderes=null;intn=0;Nodecurr=head;// Single traversalwhile(curr!=null){n++;// Every time the node number is divisible by k,// move res by oneif(n%k==0){if(res==null)res=head;elseres=res.next;}curr=curr.next;}// If n % k is not zero, then we need to take // ceil value of n / k if(n%k!=0)res=res.next;returnres.data;}staticvoidMain(){Nodehead=newNode(2);head.next=newNode(7);head.next.next=newNode(9);head.next.next.next=newNode(3);head.next.next.next.next=newNode(5);intk=3;Console.WriteLine(FractionalNode(head,k));}}
JavaScript
// JavaScript program to find fractional node in linked listclassNode{constructor(x){this.data=x;this.next=null;}}functionfractionalNode(head,k){if(head===null||k<=0)return-1;letres=null;letn=0;letcurr=head;// Single traversalwhile(curr!==null){n++;// Every time the node number is divisible by k,// move res by oneif(n%k===0){if(res===null)res=head;elseres=res.next;}curr=curr.next;}// If n % k is not zero, then we need to take // ceil value of n / k if(n%k!==0)res=res.next;returnres.data;}// Create linked listconsthead=newNode(2);head.next=newNode(7);head.next.next=newNode(9);head.next.next.next=newNode(3);head.next.next.next.next=newNode(5);constk=3;console.log(fractionalNode(head,k));