Find the fractional (or n/k - th) node in linked list
Last Updated :
02 Jun, 2025
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 - Two Traversals
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
.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
/* Function to find the fractional node
in the linked list */
int fractionalNode(Node* head, int k) {
int c = 0, d;
Node *curr = head;
// Counting total nodes
while (curr != nullptr) {
c++;
curr = curr->next;
}
// Calculating the distance
if (c % k == 0)
d = c / k;
else
d = (c / k) + 1;
int p = 1;
curr = head;
// Looping through the linked list
// to find the fractional node
while (p <= c) {
// Returning the data of the
// node at position d
if (p == d) {
return curr->data;
}
p++;
curr = curr->next;
}
return -1; // In case no fractional node is found
}
// A utility function to print a linked list
void printList(Node* node) {
while (node != nullptr) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
int main() {
Node* head = new Node(2);
head->next = new Node(7);
head->next->next = new Node(9);
head->next->next->next = new Node(3);
head->next->next->next->next = new Node(5);
int k = 3;
cout << fractionalNode(head, k);
return 0;
}
Java
// Importing necessary libraries
public class Main {
static class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
/* Function to find the fractional node
in the linked list */
static int fractionalNode(Node head, int k) {
int c = 0, d;
Node curr = head;
// Counting total nodes
while (curr != null) {
c++;
curr = curr.next;
}
// Calculating the distance
if (c % k == 0)
d = c / k;
else
d = (c / k) + 1;
int p = 1;
curr = head;
// Looping through the linked list
// to find the fractional node
while (p <= c) {
// Returning the data of the
// node at position d
if (p == d) {
return curr.data;
}
p++;
curr = curr.next;
}
return -1; // In case no fractional node is found
}
// A utility function to print a linked list
static void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(2);
head.next = new Node(7);
head.next.next = new Node(9);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(5);
int k = 3;
System.out.println(fractionalNode(head, k));
}
}
Python
# Definition for a node
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Function to find the fractional node
# in the linked list
def fractionalNode(head, k):
c = 0
curr = head
# Counting total nodes
while curr is not None:
c += 1
curr = curr.next
# Calculating the distance
d = c // k if c % k == 0 else (c // k) + 1
p = 1
curr = head
# Looping through the linked list
# to find the fractional node
while p <= c:
# Returning the data of the
# node at position d
if p == d:
return curr.data
p += 1
curr = curr.next
return -1 # In case no fractional node is found
# A utility function to print a linked list
def printList(node):
while node is not None:
print(node.data, end=' ')
node = node.next
print()
if __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 = 3
print(fractionalNode(head, k))
C#
using System;
// Definition for a node
public class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
/* Function to find the fractional node
in the linked list */
public class Program {
public static int FractionalNode(Node head, int k) {
int c = 0, d;
Node curr = head;
// Counting total nodes
while (curr != null) {
c++;
curr = curr.next;
}
// Calculating the distance
d = (c % k == 0) ? c / k : (c / k) + 1;
int p = 1;
curr = head;
// Looping through the linked list
// to find the fractional node
while (p <= c) {
// Returning the data of the
// node at position d
if (p == d) {
return curr.data;
}
p++;
curr = curr.next;
}
return -1; // In case no fractional node is found
}
// A utility function to print a linked list
public static void PrintList(Node node) {
while (node != null) {
Console.Write(node.data + " ");
node = node.next;
}
Console.WriteLine();
}
public static void Main() {
Node head = new Node(2);
head.next = new Node(7);
head.next.next = new Node(9);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(5);
int k = 3;
Console.WriteLine(FractionalNode(head, k));
}
}
JavaScript
// Definition for a node
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
/* Function to find the fractional node
in the linked list */
function fractionalNode(head, k) {
let c = 0;
let curr = head;
// Counting total nodes
while (curr !== null) {
c++;
curr = curr.next;
}
// Calculating the distance
let d = (c % k === 0) ? c / k : Math.floor(c / k) + 1;
let p = 1;
curr = head;
// Looping through the linked list
// to find the fractional node
while (p <= c) {
// Returning the data of the
// node at position d
if (p === d) {
return curr.data;
}
p++;
curr = curr.next;
}
return -1; // In case no fractional node is found
}
// A utility function to print a linked list
function printList(node) {
while (node !== null) {
process.stdout.write(node.data + ' ');
node = node.next;
}
console.log();
}
// Example usage
let head = new Node(2);
head.next = new Node(7);
head.next.next = new Node(9);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(5);
let k = 3;
console.log(fractionalNode(head, k));
Time Complexity: O(n)
Auxiliary Space: O(n)(due to the linked list storage).
Efficient Approach - One Traversal
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.
C++
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
int fractionalNode(Node* head, int k) {
if (head == nullptr || k <= 0)
return -1;
Node* res = nullptr;
int n = 0;
Node* curr = head;
// Single traversal
while (curr != nullptr) {
n++;
// Every time the node number is divisible by k,
// move res by one
if (n % k == 0) {
if (res == nullptr)
res = head;
else
res = 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;
return res->data;
}
int main() {
Node* head = new Node(2);
head->next = new Node(7);
head->next->next = new Node(9);
head->next->next->next = new Node(3);
head->next->next->next->next = new Node(5);
int k = 3;
cout << fractionalNode(head, k) << endl;
return 0;
}
Java
// Java program to find fractional node in linked list
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
static int fractionalNode(Node head, int k) {
if (head == null || k <= 0)
return -1;
Node res = null;
int n = 0;
Node curr = head;
// Single traversal
while (curr != null) {
n++;
// Every time the node number is divisible by k,
// move res by one
if (n % k == 0) {
if (res == null)
res = head;
else
res = 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;
return res.data;
}
public static void main(String[] args) {
Node head = new Node(2);
head.next = new Node(7);
head.next.next = new Node(9);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(5);
int k = 3;
System.out.println(fractionalNode(head, k));
}
}
Python
# Python program to find fractional node in linked list
class Node:
def __init__(self, x):
self.data = x
self.next = None
def fractionalNode(head, k):
if head is None or k <= 0:
return -1
res = None
n = 0
curr = head
# Single traversal
while curr is not None:
n += 1
# Every time the node number is divisible by k,
# move res by one
if n % k == 0:
if res is None:
res = head
else:
res = 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
return res.data
if __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 = 3
print(fractionalNode(head, k))
C#
// C# program to find fractional node in linked list
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
static int FractionalNode(Node head, int k) {
if (head == null || k <= 0)
return -1;
Node res = null;
int n = 0;
Node curr = head;
// Single traversal
while (curr != null) {
n++;
// Every time the node number is divisible by k,
// move res by one
if (n % k == 0) {
if (res == null)
res = head;
else
res = 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;
return res.data;
}
static void Main() {
Node head = new Node(2);
head.next = new Node(7);
head.next.next = new Node(9);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(5);
int k = 3;
Console.WriteLine(FractionalNode(head, k));
}
}
JavaScript
// JavaScript program to find fractional node in linked list
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
function fractionalNode(head, k) {
if (head === null || k <= 0)
return -1;
let res = null;
let n = 0;
let curr = head;
// Single traversal
while (curr !== null) {
n++;
// Every time the node number is divisible by k,
// move res by one
if (n % k === 0) {
if (res === null)
res = head;
else
res = 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;
return res.data;
}
// Create linked list
const head = new Node(2);
head.next = new Node(7);
head.next.next = new Node(9);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(5);
const k = 3;
console.log(fractionalNode(head, k));
Similar Reads
Insert node into the middle of the linked list Given a linked list containing n nodes. The problem is to insert a new node with data x in the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node.Examples: Input: LinkedList = 1->2->4 , x = 3Output: 1->2->3
14 min read
XOR Linked List - Find Nth Node from the end Given a XOR linked list and an integer N, the task is to print the Nth node from the end of the given XOR linked list. Examples: Input: 4 â> 6 â> 7 â> 3, N = 1 Output: 3 Explanation: 1st node from the end is 3.Input: 5 â> 8 â> 9, N = 4 Output: Wrong Input Explanation: The given Xor Li
15+ min read
Find first node of loop in a linked list Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to find the Starting node of the loop in the linked list if there is no loop in the linked list return -1.Example: Input: Output: 3Exp
14 min read
Delete Nth node from the end of the given linked list Given a linked list and an integer N, the task is to delete the Nth node from the end of the given linked list.Examples: Input: 2 -> 3 -> 1 -> 7 -> NULL, N = 1 Output: 2 3 1Explanation: The created linked list is: 2 3 1 7 The linked list after deletion is: 2 3 1Input: 1 -> 2 -> 3 -
10 min read
Squareroot(n)-th node in a Linked List Given a Linked List, write a function that accepts the head node of the linked list as a parameter and returns the value of node present at (floor(sqrt(n)))th position in the Linked List, where n is the length of the linked list or the total number of nodes in the list. Examples: Input: 1->2->
9 min read
Write a function to get Nth node in a Linked List Given a LinkedList and an index (1-based). The task is to find the data value stored in the node at that kth position. If no such node exists whose index is k then return -1.Example:Â Input: 1->10->30->14, index = 2Output: 10Explanation: The node value at index 2 is 10 Input: 1->32->12
11 min read