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.
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 til 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.
Given linked list is:10 20 30 40 sqrt(n)th node is 20
This article is contributed by Akshit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find kth node from Middle towards Head of a Linked List
- Sublist Search (Search a linked list in another list)
- Minimum number of reversals to reach node 0 from every other node
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Farthest distance of a Node from each Node of a Tree
- Move all occurrences of an element to end in a linked list
- Maximum occurring character in a linked list
- Binary Search on Singly Linked List
- Check if a pair with given product exists in Linked list
- Count duplicates in a given linked list
- Absolute distinct count in a Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- First common element in two linked lists
- Hashtables Chaining with Doubly Linked Lists
- Find Leftmost and Rightmost node of BST from its given preorder traversal
- Find the color of given node in an infinite binary tree
- Find parent of given node in a Binary Tree with given postorder traversal
- Find the node at the centre of an N-ary tree
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Diameters for each node of Tree after connecting it with given disconnected component