Squareroot(n)-th node in a Linked List
Last Updated :
25 Oct, 2022
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->3->4->5->NULL
Output: 2
Input : 10->20->30->40->NULL
Output : 20
Input : 10->20->30->40->50->60->70->80->90->NULL
Output : 30
Simple method: The simple method is to first find the total number of nodes present in the linked list, then find the value of floor(squareroot(n)) where n is the total number of nodes. Then traverse from the first node in the list to this position and return the node at this position.
This method traverses the linked list 2 times.
Optimized approach: In this method, we can get the required node by traversing the linked list once only. Below is the step by step algorithm for this approach.
- Initialize two counters i and j both to 1 and a pointer sqrtn to NULL to traverse till the required position is reached.
- Start traversing the list using head node until the last node is reached.
- While traversing check if the value of j is equal to sqrt(i). If the value is equal increment both i and j and sqrtn to point sqrtn->next otherwise increment only i.
- Now, when we will reach the last node of list i will contain value of n, j will contain value of sqrt(i) and sqrtn will point to node at jth position.
C++
// C++ program to find sqrt(n)'th node
// of a linked list
#include <bits/stdc++.h>
using namespace std;
// Linked list node
class Node
{
public:
int data;
Node* next;
};
// Function to get the sqrt(n)th
// node of a linked list
int printsqrtn(Node* head)
{
Node* sqrtn = NULL;
int i = 1, j = 1;
// Traverse the list
while (head!=NULL)
{
// check if j = sqrt(i)
if (i == j*j)
{
// for first node
if (sqrtn == NULL)
sqrtn = head;
else
sqrtn = sqrtn->next;
// increment j if j = sqrt(i)
j++;
}
i++;
head=head->next;
}
// return node's data
return sqrtn->data;
}
void print(Node* head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout<<endl;
}
// function to add a new node at the
// beginning of the list
void push(Node** head_ref, int new_data)
{
// allocate node
Node* new_node = new Node();
// put in the data
new_node->data = new_data;
// link the old list of the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
Node* head = NULL;
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
cout << "Given linked list is:";
print(head);
cout << "sqrt(n)th node is " << printsqrtn(head);
return 0;
}
// This is code is contributed by rathbhupendra
C
// C program to find sqrt(n)'th node
// of a linked list
#include<stdio.h>
#include<stdlib.h>
// Linked list node
struct Node
{
int data;
struct Node* next;
};
// Function to get the sqrt(n)th
// node of a linked list
int printsqrtn(struct Node* head)
{
struct Node* sqrtn = NULL;
int i = 1, j = 1;
// Traverse the list
while (head!=NULL)
{
// check if j = sqrt(i)
if (i == j*j)
{
// for first node
if (sqrtn == NULL)
sqrtn = head;
else
sqrtn = sqrtn->next;
// increment j if j = sqrt(i)
j++;
}
i++;
head=head->next;
}
// return node's data
return sqrtn->data;
}
void print(struct Node* head)
{
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
// function to add a new node at the
// beginning of the list
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
// put in the data
new_node->data = new_data;
// link the old list of the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
printf("Given linked list is:");
print(head);
printf("sqrt(n)th node is %d ",printsqrtn(head));
return 0;
}
Java
// Java program to find sqrt(n)'th node
// of a linked list
class GfG
{
// Linked list node
static class Node
{
int data;
Node next;
}
static Node head = null;
// Function to get the sqrt(n)th
// node of a linked list
static int printsqrtn(Node head)
{
Node sqrtn = null;
int i = 1, j = 1;
// Traverse the list
while (head != null)
{
// check if j = sqrt(i)
if (i == j * j)
{
// for first node
if (sqrtn == null)
sqrtn = head;
else
sqrtn = sqrtn.next;
// increment j if j = sqrt(i)
j++;
}
i++;
head=head.next;
}
// return node's data
return sqrtn.data;
}
static void print(Node head)
{
while (head != null)
{
System.out.print( head.data + " ");
head = head.next;
}
System.out.println();
}
// function to add a new node at the
// beginning of the list
static void push( int new_data)
{
// allocate node
Node new_node = new Node();
// put in the data
new_node.data = new_data;
// link the old list of the new node
new_node.next = head;
// move the head to point to the new node
head = new_node;
}
/* Driver code*/
public static void main(String[] args)
{
/* Start with the empty list */
push( 40);
push( 30);
push( 20);
push( 10);
System.out.print("Given linked list is:");
print(head);
System.out.print("sqrt(n)th node is " +
printsqrtn(head));
}
}
// This code is contributed by prerna saini
Python3
# Python3 program to find sqrt(n)'th node
# of a linked list
# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data
self.next = None
# Function to get the sqrt(n)th
# node of a linked list
def printsqrtn(head) :
sqrtn = None
i = 1
j = 1
# Traverse the list
while (head != None) :
# check if j = sqrt(i)
if (i == j * j) :
# for first node
if (sqrtn == None) :
sqrtn = head
else:
sqrtn = sqrtn.next
# increment j if j = sqrt(i)
j = j + 1
i = i + 1
head = head.next
# return node's data
return sqrtn.data
def print_1(head) :
while (head != None) :
print( head.data, end = " ")
head = head.next
print(" ")
# function to add a new node at the
# beginning of the list
def push(head_ref, new_data) :
# allocate node
new_node = Node(0)
# put in the data
new_node.data = new_data
# link the old list of the new node
new_node.next = (head_ref)
# move the head to point to the new node
(head_ref) = new_node
return head_ref
# Driver Code
if __name__=='__main__':
# Start with the empty list
head = None
head = push(head, 40)
head = push(head, 30)
head = push(head, 20)
head = push(head, 10)
print("Given linked list is:")
print_1(head)
print("sqrt(n)th node is ",
printsqrtn(head))
# This code is contributed by Arnab Kundu
C#
// C# program to find sqrt(n)'th node
// of a linked list
using System;
public class GfG
{
// Linked list node
class Node
{
public int data;
public Node next;
}
static Node head = null;
// Function to get the sqrt(n)th
// node of a linked list
static int printsqrtn(Node head)
{
Node sqrtn = null;
int i = 1, j = 1;
// Traverse the list
while (head != null)
{
// check if j = sqrt(i)
if (i == j * j)
{
// for first node
if (sqrtn == null)
sqrtn = head;
else
sqrtn = sqrtn.next;
// increment j if j = sqrt(i)
j++;
}
i++;
head=head.next;
}
// return node's data
return sqrtn.data;
}
static void print(Node head)
{
while (head != null)
{
Console.Write( head.data + " ");
head = head.next;
}
Console.WriteLine();
}
// function to add a new node at the
// beginning of the list
static void push( int new_data)
{
// allocate node
Node new_node = new Node();
// put in the data
new_node.data = new_data;
// link the old list of the new node
new_node.next = head;
// move the head to point to the new node
head = new_node;
}
/* Driver code*/
public static void Main(String[] args)
{
/* Start with the empty list */
push( 40);
push( 30);
push( 20);
push( 10);
Console.Write("Given linked list is:");
print(head);
Console.Write("sqrt(n)th node is " +
printsqrtn(head));
}
}
/* This code is contributed by 29AjayKumar */
JavaScript
<script>
// JavaScript program to find sqrt(n)'th node
// of a linked list
// Linked list node
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
var head = null;
// Function to get the sqrt(n)th
// node of a linked list
function printsqrtn(head) {
var sqrtn = null;
var i = 1, j = 1;
// Traverse the list
while (head != null) {
// check if j = sqrt(i)
if (i == j * j) {
// for first node
if (sqrtn == null)
sqrtn = head;
else
sqrtn = sqrtn.next;
// increment j if j = sqrt(i)
j++;
}
i++;
head = head.next;
}
// return node's data
return sqrtn.data;
}
function print(head) {
while (head != null) {
document.write(head.data + " ");
head = head.next;
}
document.write("<br/>");
}
// function to add a new node at the
// beginning of the list
function push(new_data) {
// allocate node
var new_node = new Node();
// put in the data
new_node.data = new_data;
// link the old list of the new node
new_node.next = head;
// move the head to point to the new node
head = new_node;
}
/* Driver code */
/* Start with the empty list */
push(40);
push(30);
push(20);
push(10);
document.write("Given linked list is:");
print(head);
document.write("sqrt(n)th node is " + printsqrtn(head));
// This code contributed by Rajput-Ji
</script>
Output:
Given linked list is:10 20 30 40
sqrt(n)th node is 20
Complexity Analysis:
Time Complexity: O(n).
Space Complexity: O(1).
Similar Reads
Rotate Doubly linked list by N nodes Given a doubly-linked list, the task is to rotate the linked list counter-clockwise by p nodes. Here p is a given positive integer and is smaller than the count of nodes in the linked list. Examples:Input: Output: Explanation: After rotating the list by p = 2, the new head will be the node with valu
11 min read
Insert Node at the End of a Linked List Given a linked list, the task is to insert a new node at the end of the linked list.Examples:Input: LinkedList = 2 -> 3 -> 4 -> 5, NewNode = 1Output: LinkedList = 2 -> 3 -> 4 -> 5 -> 1Input: LinkedList = NULL, NewNode = 1Output: LinkedList = 1Approach:Â Inserting at the end invol
9 min read
Find the fractional (or n/k - th) node in linked list 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 = 2Output: 3Explanation: 6/2th element is the 3rd(1-based i
11 min read
Find Middle of the Linked List Given a singly linked list, the task is to find the middle of the linked list. If the number of nodes are even, then there would be two middle nodes, so return the second middle node.Example:Input: linked list: 1->2->3->4->5Output: 3 Explanation: There are 5 nodes in the linked list and
14 min read
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
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 : 9Recommended PracticeModular NodeTry It!Ta
5 min read