Find modular node in a linked list
Given a singly linked list and a number k, find the last node whose n%k == 0, where n is the number of nodes in the list.
Examples:
Input : list = 1->2->3->4->5->6->7
k = 3
Output : 6
Input : list = 3->7->1->9->8
k = 2
Output : 9
- Take a pointer modularNode and initialize it with NULL. Traverse the linked list.
- For every i%k=0, update modularNode.
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* modularNode(Node* head, int k)
{
if (k <= 0 || head == NULL)
return NULL;
int i = 1;
Node* modularNode = NULL;
for (Node* temp = head; temp != NULL; temp = temp->next) {
if (i % k == 0)
modularNode = temp;
i++;
}
return modularNode;
}
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;
Node* answer = modularNode(head, k);
printf ( "\nModular node is " );
if (answer != NULL)
printf ( "%d\n" , answer->data);
else
printf ( "null\n" );
return 0;
}
|
Java
public class GFG
{
static class Node{
int data;
Node next;
Node( int data){
this .data = data;
}
}
static Node modularNode(Node head, int k)
{
if (k <= 0 || head == null )
return null ;
int i = 1 ;
Node modularNode = null ;
for (Node temp = head; temp != null ; temp = temp.next) {
if (i % k == 0 )
modularNode = temp;
i++;
}
return modularNode;
}
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 ;
Node answer = modularNode(head, k);
System.out.print( "Modular node is " );
if (answer != null )
System.out.println(answer.data);
else
System.out.println( "null" );
}
}
|
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 modularNode(head, k):
if (k < = 0 or head = = None ):
return None
i = 1
modularNode = None
temp = head
while (temp ! = None ):
if (i % k = = 0 ):
modularNode = temp
i = i + 1
temp = temp. next
return modularNode
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
answer = modularNode(head, k)
print ( "Modular node is" , end = ' ' )
if (answer ! = None ):
print (answer.data, end = ' ' )
else :
print ( "None" )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
}
}
static Node modularNode(Node head, int k)
{
if (k <= 0 || head == null )
return null ;
int i = 1;
Node modularNode = null ;
for (Node temp = head; temp != null ; temp = temp.next)
{
if (i % k == 0)
modularNode = temp;
i++;
}
return modularNode;
}
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;
Node answer = modularNode(head, k);
Console.Write( "Modular node is " );
if (answer != null )
Console.WriteLine(answer.data);
else
Console.WriteLine( "null" );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function modularNode(head , k) {
if (k <= 0 || head == null )
return null ;
var i = 1;
var modularNode = null ;
for (temp = head; temp != null ; temp = temp.next) {
if (i % k == 0)
modularNode = temp;
i++;
}
return modularNode;
}
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;
var answer = modularNode(head, k);
document.write( "Modular node is " );
if (answer != null )
document.write(answer.data);
else
document.write( "null" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n).
- Space complexity: O(1).
Last Updated :
15 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...