Node at Given Index in Linked List

Last Updated : 28 Jul, 2026

Given the head of a singly linked list and an integer k, return the data of the node present at the k-th position using 1-based indexing. Return -1 if the k-th node does not exist.

Examples:

Input: k = 3,

test-1
head of a Linked List

Output: 3
Explanation: The node value at index 3 is 3.

Input: k = 6,
test-2Output: -1
Explanation: The linked list contains fewer than 6 nodes, so no node exists at index 6.

Try It Yourself
redirect icon

[Naive Approach] Store Node Values - O(n) Time and O(n) Space

The idea is - Traverse the linked list and store the value of every node in an array. Since the list uses 1-based indexing while the array uses 0-based indexing, the required node value will be stored at index k - 1. If k is greater than the number of nodes, return -1.

C++
#include <iostream>
#include <vector>
using namespace std;

struct Node {
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

int getNode(Node* head, int k) {
    vector<int> values;

    // Store the data of every node.
    while (head != nullptr) {
        values.push_back(head->data);
        head = head->next;
    }

    if (k > 0 && k <= static_cast<int>(values.size())) {
        return values[k - 1];
    }

    return -1;
}

int main() {
    Node* head1 = new Node(1);
    head1->next = new Node(2);
    head1->next->next = new Node(3);
    head1->next->next->next = new Node(4);
    head1->next->next->next->next = new Node(5);
    head1->next->next->next->next->next = new Node(6);
    head1->next->next->next->next->next->next = new Node(7);

    cout << getNode(head1, 3) << "\n";

    Node* head2 = new Node(19);
    head2->next = new Node(28);
    head2->next->next = new Node(37);
    head2->next->next->next = new Node(48);
    head2->next->next->next->next = new Node(55);

    cout << getNode(head2, 6) << "\n";

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

class GFG {
    static int getNode(Node head, int k) {
        java.util.ArrayList<Integer> values = new java.util.ArrayList<>();

        // Store the data of every node.
        while (head != null) {
            values.add(head.data);
            head = head.next;
        }

        if (k > 0 && k <= values.size()) {
            return values.get(k - 1);
        }

        return -1;
    }

    public static void main(String[] args) {
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        System.out.println(getNode(head1, 3));

        Node head2 = new Node(19);
        head2.next = new Node(28);
        head2.next.next = new Node(37);
        head2.next.next.next = new Node(48);
        head2.next.next.next.next = new Node(55);

        System.out.println(getNode(head2, 6));
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def getNode(head, k):
    values = []

    # Store the data of every node.
    while head is not None:
        values.append(head.data)
        head = head.next

    if 0 < k <= len(values):
        return values[k - 1]

    return -1


if __name__ == "__main__":
    head1 = Node(1)
    head1.next = Node(2)
    head1.next.next = Node(3)
    head1.next.next.next = Node(4)
    head1.next.next.next.next = Node(5)
    head1.next.next.next.next.next = Node(6)
    head1.next.next.next.next.next.next = Node(7)

    print(getNode(head1, 3))

    head2 = Node(19)
    head2.next = Node(28)
    head2.next.next = Node(37)
    head2.next.next.next = Node(48)
    head2.next.next.next.next = Node(55)

    print(getNode(head2, 6))
C#
using System;
using System.Collections.Generic;

public class Node
{
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG
{
    static int getNode(Node head, int k)
    {
        List<int> values = new List<int>();

        // Store the data of every node.
        while (head != null)
        {
            values.Add(head.data);
            head = head.next;
        }

        if (k > 0 && k <= values.Count)
        {
            return values[k - 1];
        }

        return -1;
    }

    static void Main()
    {
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        Console.WriteLine(getNode(head1, 3));

        Node head2 = new Node(19);
        head2.next = new Node(28);
        head2.next.next = new Node(37);
        head2.next.next.next = new Node(48);
        head2.next.next.next.next = new Node(55);

        Console.WriteLine(getNode(head2, 6));
    }
}
JavaScript
'use strict';

class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

/**
 * @param {Node} head
 * @param {number} k
 * @return {number}
 */
function getNode(head, k) {
    const values = [];

    // Store the data of every node.
    while (head !== null) {
        values.push(head.data);
        head = head.next;
    }

    if (k > 0 && k <= values.length) {
        return values[k - 1];
    }

    return -1;
}

// Driver Code
const head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);

console.log(getNode(head1, 3));

const head2 = new Node(19);
head2.next = new Node(28);
head2.next.next = new Node(37);
head2.next.next.next = new Node(46);
head2.next.next.next.next = new Node(55);

console.log(getNode(head2, 6));

Output
3
-1

[Expected Approach] Iterative Traversal - O(k) Time and O(1) Space

The idea is - Start traversal from the head of the linked list and maintain a position counter initialized to 1.

For every node:

  • If the current position is equal to k, return the current node's data.
  • Otherwise, move to the next node and increase the position.

If the traversal ends before reaching position k, return -1. This approach does not require an extra array or recursion stack.

Time Complexity: O(k) in the valid case and O(n) when the k-th node does not exist.

C++
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

int getNode(Node* head, int k) {
    int position = 1;

    // Traverse until the k-th node is found.
    while (head != nullptr) {
        if (position == k) {
            return head->data;
        }

        head = head->next;
        position++;
    }

    return -1;
}

int main() {
    Node* head1 = new Node(1);
    head1->next = new Node(2);
    head1->next->next = new Node(3);
    head1->next->next->next = new Node(4);
    head1->next->next->next->next = new Node(5);
    head1->next->next->next->next->next = new Node(6);
    head1->next->next->next->next->next->next = new Node(7);

    cout << getNode(head1, 3) << "\n";

    Node* head2 = new Node(19);
    head2->next = new Node(28);
    head2->next->next = new Node(37);
    head2->next->next->next = new Node(48);
    head2->next->next->next->next = new Node(55);

    cout << getNode(head2, 6) << "\n";

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

class GFG {
    static int getNode(Node head, int k) {
        int position = 1;

        // Traverse until the k-th node is found.
        while (head != null) {
            if (position == k) {
                return head.data;
            }

            head = head.next;
            position++;
        }

        return -1;
    }

    public static void main(String[] args) {
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        System.out.println(getNode(head1, 3));

        Node head2 = new Node(19);
        head2.next = new Node(28);
        head2.next.next = new Node(37);
        head2.next.next.next = new Node(48);
        head2.next.next.next.next = new Node(55);

        System.out.println(getNode(head2, 6));
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def getNode(head, k):
    position = 1

    # Traverse until the k-th node is found.
    while head is not None:
        if position == k:
            return head.data

        head = head.next
        position += 1

    return -1


if __name__ == "__main__":
    head1 = Node(1)
    head1.next = Node(2)
    head1.next.next = Node(3)
    head1.next.next.next = Node(4)
    head1.next.next.next.next = Node(5)
    head1.next.next.next.next.next = Node(6)
    head1.next.next.next.next.next.next = Node(7)

    print(getNode(head1, 3))

    head2 = Node(19)
    head2.next = Node(28)
    head2.next.next = Node(37)
    head2.next.next.next = Node(48)
    head2.next.next.next.next = Node(55)

    print(getNode(head2, 6))
C#
using System;

public class Node
{
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG
{
    static int getNode(Node head, int k)
    {
        int position = 1;

        // Traverse until the k-th node is found.
        while (head != null)
        {
            if (position == k)
            {
                return head.data;
            }

            head = head.next;
            position++;
        }

        return -1;
    }

    static void Main()
    {
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        Console.WriteLine(getNode(head1, 3));

        Node head2 = new Node(19);
        head2.next = new Node(28);
        head2.next.next = new Node(37);
        head2.next.next.next = new Node(48);
        head2.next.next.next.next = new Node(55);

        Console.WriteLine(getNode(head2, 6));
    }
}
JavaScript
'use strict';

class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

/**
 * @param {Node} head
 * @param {number} k
 * @return {number}
 */
function getNode(head, k) {
    let position = 1;

    // Traverse until the k-th node is found.
    while (head !== null) {
        if (position === k) {
            return head.data;
        }

        head = head.next;
        position++;
    }

    return -1;
}

// Driver Code
const head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);

console.log(getNode(head1, 3));

const head2 = new Node(19);
head2.next = new Node(28);
head2.next.next = new Node(37);
head2.next.next.next = new Node(46);
head2.next.next.next.next = new Node(55);

console.log(getNode(head2, 6));

Output
3
-1
Comment