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).
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.