Open In App

Write a function to get Nth node in a Linked List

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a LinkedList and an index (1-based). The task is to find the data value stored in the node at that kth position. If no such node exists whose index is k then return -1.

Example: 

Input: 1->10->30->14, index = 2
Output: 10
Explanation: The node value at index 2 is 10

Maximum-of-all-subarrays-of-size-K


Input: 1->32->12->10->30->14->100, index = 8
Output: -1
Explanation: No such node exists at index = 8.

[Naive Approach] Recursive Method – O(n) Time and O(n) Space

The idea is to use the recursive method to find the value of index node (1- based) . Call the function GetNth(head,index) recusively, where head will represent the current head node . Decrement the index value by 1 on every recursion call. When the n reaches 1 ,we will return the data of current node.

Below is the implementation of above approach: 

C++
//C++ program to find the data at nth node
//recursively

#include <bits/stdc++.h>
using namespace std;
struct Node {
    int data;
    Node* next;
    Node(int x) {
      data = x;
      next = NULL;
    }
};

// Takes head pointer of the linked list and index
// as arguments and returns data at index.
int GetNth(Node* head, int index) {
  
    // If the list is empty or index is out of bounds
    if (head == NULL)
        return -1;

    // If index equals 1, return node's data
    if (index == 1)
        return head->data;

    // Recursively move to the next node
    return GetNth(head->next, index - 1);
}


int main() {
  
    // Create a hard-coded linked list:
    // 1 -> 2 -> 3 -> 4 -> 5
    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);

    cout << "Element at index 3 is " << GetNth(head, 3) << endl;

    return 0;
}
C Java Python C# JavaScript

Output
Element at index 3 is 3

Time Complexity : O(n) ,where n is the nth node of linked list.
Auxiliary Space: O(n), for recursive call stack

[Expected Approach-2] Iterative Method – O(n) Time and O(1) Space

The idea is similar to recursive approach to find the value at index node (1- based) .We will use a variable say, count = 1 to track the nodes. Traverse the list until curr != NULL . Increment the count if count is not equal to index node (1- based) , else if count equals to the index node, return data at current node.

Below is the implementation of above approach :

C++
// C++ program to find n'th
// node in linked list (iteratively)

#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int x) {
        data = x;
        next = nullptr;
    }
};

// Function to find the nth node in the list
int GetNth(Node *head, int index) {
    Node *curr = head;
    int count = 1;

    while (curr != nullptr) {
        if (count == index)
            return curr->data;
        count++;
        curr = curr->next;
    }

    return -1;
}

int main() {
  
    // Create a hard-coded linked list:
    // 1 -> 2 -> 3 -> 4 -> 5
    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);

    cout << "Element at index 3 is " << GetNth(head, 3) << endl;

    return 0;
}
C Java Python C# JavaScript

Output
Element at index 3 is 3

Time Complexity : O(n), where n is the nth node of linked list.
Auxiliary Space: O(1)



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg