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 : list = 1->2->3->4->5->6
k = 2
Output : 3
Since n = 6 and k = 2, we print (6/2)-th node
which is 3.
Input : list = 2->7->9->3->5
k = 3
Output : 7
Since n is 5 and k is 3, we print ceil(5/3)-th
node which is 2nd node, i.e., 7.
- Take two pointers temp and fractionalNode and initialize them with null and head respectively.
- For every k jumps of the temp pointer, make one jump of the fractionalNode pointer.
Implementation:
C++
#include <bits/stdc++.h>
struct Node {
int data;
Node* next;
};
Node* newNode( int data)
{
Node* new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
Node* fractionalNodes(Node* head, int k)
{
if (k <= 0 || head == NULL)
return NULL;
Node* fractionalNode = NULL;
int i = 0;
for (Node* temp = head; temp != NULL; temp = temp->next) {
if (i % k == 0) {
if (fractionalNode == NULL)
fractionalNode = head;
else
fractionalNode = fractionalNode->next;
}
i++;
}
return fractionalNode;
}
void printList(Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
printf ( "\n" );
}
int main( void )
{
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
int k = 2;
printf ( "List is " );
printList(head);
Node* answer = fractionalNodes(head, k);
printf ( "\nFractional node is " );
printf ( "%d\n" , answer->data);
return 0;
}
|
Java
public class FractionalNodell
{
static class Node{
int data;
Node next;
Node ( int data){
this .data = data;
}
}
static Node fractionalNodes(Node head, int k)
{
if (k <= 0 || head == null )
return null ;
Node fractionalNode = null ;
int i = 0 ;
for (Node temp = head; temp != null ;
temp = temp.next){
if (i % k == 0 ){
if (fractionalNode == null )
fractionalNode = head;
else
fractionalNode = fractionalNode.next;
}
i++;
}
return fractionalNode;
}
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( 1 );
head.next = new Node( 2 );
head.next.next = new Node( 3 );
head.next.next.next = new Node( 4 );
head.next.next.next.next = new Node( 5 );
int k = 2 ;
System.out.print( "List is " );
printList(head);
Node answer = fractionalNodes(head, k);
System.out.println( "Fractional node is " +
answer.data);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(data):
new_node = Node(data)
new_node.data = data
new_node. next = None
return new_node
def fractionalNodes(head, k):
if (k < = 0 or head = = None ):
return None
fractionalNode = None
i = 0
temp = head
while (temp ! = None ):
if (i % k = = 0 ):
if (fractionalNode = = None ):
fractionalNode = head
else :
fractionalNode = fractionalNode. next
i = i + 1
temp = temp. next
return fractionalNode
def printList(node):
while (node ! = None ):
print (node.data, end = ' ' )
node = node. next
if __name__ = = '__main__' :
head = newNode( 1 )
head. next = newNode( 2 )
head. next . next = newNode( 3 )
head. next . next . next = newNode( 4 )
head. next . next . next . next = newNode( 5 )
k = 2
print ( "List is" , end = ' ' )
printList(head)
answer = fractionalNodes(head, k)
print ( "\nFractional node is" , end = ' ' )
print (answer.data)
|
C#
using System;
public class FractionalNodell
{
public class Node
{
public int data;
public Node next;
public Node ( int data)
{
this .data = data;
}
}
static Node fractionalNodes(Node head, int k)
{
if (k <= 0 || head == null )
return null ;
Node fractionalNode = null ;
int i = 0;
for (Node temp = head; temp != null ;
temp = temp.next)
{
if (i % k == 0)
{
if (fractionalNode == null )
fractionalNode = head;
else
fractionalNode = fractionalNode.next;
}
i++;
}
return fractionalNode;
}
static void printList(Node node)
{
while (node != null )
{
Console.Write(node.data+ " " );
node = node.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
int k =2;
Console.Write( "List is " );
printList(head);
Node answer = fractionalNodes(head, k);
Console.WriteLine( "Fractional node is " +
answer.data);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function fractionalNodes(head , k) {
if (k <= 0 || head == null )
return null ;
var fractionalNode = null ;
var i = 0;
for (temp = head; temp != null ; temp = temp.next) {
if (i % k == 0) {
if (fractionalNode == null )
fractionalNode = head;
else
fractionalNode = fractionalNode.next;
}
i++;
}
return fractionalNode;
}
function printList(node) {
while (node != null ) {
document.write(node.data + " " );
node = node.next;
}
document.write();
}
var head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
var k = 2;
document.write( "List is " );
printList(head);
var answer = fractionalNodes(head, k);
document.write( "<br/>Fractional node is " + answer.data);
</script>
|
OutputList is 1 2 3 4 5
Fractional node is 3
Complexity Analysis
- Time Complexity: O(n)
- Space complexity: O(1)
This article is contributed by Prakriti Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.