Squareroot(n)-th node in a Linked List

2

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.

  1. Initialize two counters i and j both to 1 and a pointer sqrtn to NULL to traverse til the required position is reached.
  2. Start traversing the list using head node until the last node is reached.
  3. 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.
  4. 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 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 
// begining 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 off 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;
}

Output:

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 contribute@geeksforgeeks.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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.