
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the fractional (or n/k – th) node in linked list in C++
Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.
To solve this we have to follow some steps like below −
- Take two pointers called temp, and fracPoint, then initialize them with null and start respectively.
- For every k, jumps of the temp pointer, make one jump of the fracPoint pointer.
Example
#include<iostream> using namespace std; class Node { public: int data; Node* next; }; Node* getNode(int data) { Node* new_node = new Node; new_node->data = data; new_node->next = NULL; return new_node; } Node* fractionalNodes(Node* start, int k) { if (k <= 0 || start == NULL) return NULL; Node* fracPoint = NULL; int i = 0; for (Node* temp = start; temp != NULL; temp = temp->next) { if (i % k == 0) { if (fracPoint == NULL) fracPoint = start; else fracPoint = fracPoint->next; } i++; } return fracPoint; } void printList(Node* node) { while (node != NULL) { cout << node->data << " "; node = node->next; } cout << endl; } int main(void) { Node* start = getNode(1); start->next = getNode(2); start->next->next = getNode(3); start->next->next->next = getNode(4); start->next->next->next->next = getNode(5); int k = 2; cout << "List is: "; printList(start); Node* answer = fractionalNodes(start, k); cout << "\nFractional node is " << answer->data; }
Output
List is: 1 2 3 4 5 Fractional node is 3
- Related Articles
- Remove Every K-th Node Of The Linked List
- Program to find the K-th last node of a linked list in Python
- Golang Program to delete the node after the Kth node (K is not in the linked list).
- Find the largest node in Doubly linked list in C++
- C# program to find node in Linked List
- Find modular node in a linked list in C++
- Find n-th node of inorder traversal in C++
- Golang program to insert a new node after the Kth node (K is not in the linked list)
- Golang Program to update the node value after the Kth node (When K is not in the linked list).
- Linked List Random Node in C++
- Delete Node in a Linked List in Python
- Program to find the middle node of a singly linked list in Python
- Find the second last node of a linked list in single traversal in C++
- Find n-th node in Postorder traversal of a Binary Tree in C++
- Find n-th node in Preorder traversal of a Binary Tree in C++

Advertisements