Open In App

Find the fractional (or n/k - th) node in linked list

Last Updated : 02 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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

Output
7

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

Output
7

Article Tags :
Practice Tags :

Similar Reads