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,
head of a Linked List
Output: 3 Explanation: The node value at index 3 is 3.
Input: k = 6, Output: -1 Explanation: The linked list contains fewer than 6 nodes, so no node exists at index 6.
[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>usingnamespacestd;structNode{intdata;Node*next;Node(intx){data=x;next=nullptr;}};intgetNode(Node*head,intk){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())){returnvalues[k-1];}return-1;}intmain(){Node*head1=newNode(1);head1->next=newNode(2);head1->next->next=newNode(3);head1->next->next->next=newNode(4);head1->next->next->next->next=newNode(5);head1->next->next->next->next->next=newNode(6);head1->next->next->next->next->next->next=newNode(7);cout<<getNode(head1,3)<<"\n";Node*head2=newNode(19);head2->next=newNode(28);head2->next->next=newNode(37);head2->next->next->next=newNode(48);head2->next->next->next->next=newNode(55);cout<<getNode(head2,6)<<"\n";return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGFG{staticintgetNode(Nodehead,intk){java.util.ArrayList<Integer>values=newjava.util.ArrayList<>();// Store the data of every node.while(head!=null){values.add(head.data);head=head.next;}if(k>0&&k<=values.size()){returnvalues.get(k-1);}return-1;}publicstaticvoidmain(String[]args){Nodehead1=newNode(1);head1.next=newNode(2);head1.next.next=newNode(3);head1.next.next.next=newNode(4);head1.next.next.next.next=newNode(5);head1.next.next.next.next.next=newNode(6);head1.next.next.next.next.next.next=newNode(7);System.out.println(getNode(head1,3));Nodehead2=newNode(19);head2.next=newNode(28);head2.next.next=newNode(37);head2.next.next.next=newNode(48);head2.next.next.next.next=newNode(55);System.out.println(getNode(head2,6));}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefgetNode(head,k):values=[]# Store the data of every node.whileheadisnotNone:values.append(head.data)head=head.nextif0<k<=len(values):returnvalues[k-1]return-1if__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#
usingSystem;usingSystem.Collections.Generic;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGFG{staticintgetNode(Nodehead,intk){List<int>values=newList<int>();// Store the data of every node.while(head!=null){values.Add(head.data);head=head.next;}if(k>0&&k<=values.Count){returnvalues[k-1];}return-1;}staticvoidMain(){Nodehead1=newNode(1);head1.next=newNode(2);head1.next.next=newNode(3);head1.next.next.next=newNode(4);head1.next.next.next.next=newNode(5);head1.next.next.next.next.next=newNode(6);head1.next.next.next.next.next.next=newNode(7);Console.WriteLine(getNode(head1,3));Nodehead2=newNode(19);head2.next=newNode(28);head2.next.next=newNode(37);head2.next.next.next=newNode(48);head2.next.next.next.next=newNode(55);Console.WriteLine(getNode(head2,6));}}
JavaScript
'use strict';classNode{constructor(x){this.data=x;this.next=null;}}/** * @param {Node} head * @param {number} k * @return {number} */functiongetNode(head,k){constvalues=[];// Store the data of every node.while(head!==null){values.push(head.data);head=head.next;}if(k>0&&k<=values.length){returnvalues[k-1];}return-1;}// Driver Codeconsthead1=newNode(1);head1.next=newNode(2);head1.next.next=newNode(3);head1.next.next.next=newNode(4);head1.next.next.next.next=newNode(5);head1.next.next.next.next.next=newNode(6);head1.next.next.next.next.next.next=newNode(7);console.log(getNode(head1,3));consthead2=newNode(19);head2.next=newNode(28);head2.next.next=newNode(37);head2.next.next.next=newNode(46);head2.next.next.next.next=newNode(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>usingnamespacestd;structNode{intdata;Node*next;Node(intx){data=x;next=nullptr;}};intgetNode(Node*head,intk){intposition=1;// Traverse until the k-th node is found.while(head!=nullptr){if(position==k){returnhead->data;}head=head->next;position++;}return-1;}intmain(){Node*head1=newNode(1);head1->next=newNode(2);head1->next->next=newNode(3);head1->next->next->next=newNode(4);head1->next->next->next->next=newNode(5);head1->next->next->next->next->next=newNode(6);head1->next->next->next->next->next->next=newNode(7);cout<<getNode(head1,3)<<"\n";Node*head2=newNode(19);head2->next=newNode(28);head2->next->next=newNode(37);head2->next->next->next=newNode(48);head2->next->next->next->next=newNode(55);cout<<getNode(head2,6)<<"\n";return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGFG{staticintgetNode(Nodehead,intk){intposition=1;// Traverse until the k-th node is found.while(head!=null){if(position==k){returnhead.data;}head=head.next;position++;}return-1;}publicstaticvoidmain(String[]args){Nodehead1=newNode(1);head1.next=newNode(2);head1.next.next=newNode(3);head1.next.next.next=newNode(4);head1.next.next.next.next=newNode(5);head1.next.next.next.next.next=newNode(6);head1.next.next.next.next.next.next=newNode(7);System.out.println(getNode(head1,3));Nodehead2=newNode(19);head2.next=newNode(28);head2.next.next=newNode(37);head2.next.next.next=newNode(48);head2.next.next.next.next=newNode(55);System.out.println(getNode(head2,6));}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefgetNode(head,k):position=1# Traverse until the k-th node is found.whileheadisnotNone:ifposition==k:returnhead.datahead=head.nextposition+=1return-1if__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#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGFG{staticintgetNode(Nodehead,intk){intposition=1;// Traverse until the k-th node is found.while(head!=null){if(position==k){returnhead.data;}head=head.next;position++;}return-1;}staticvoidMain(){Nodehead1=newNode(1);head1.next=newNode(2);head1.next.next=newNode(3);head1.next.next.next=newNode(4);head1.next.next.next.next=newNode(5);head1.next.next.next.next.next=newNode(6);head1.next.next.next.next.next.next=newNode(7);Console.WriteLine(getNode(head1,3));Nodehead2=newNode(19);head2.next=newNode(28);head2.next.next=newNode(37);head2.next.next.next=newNode(48);head2.next.next.next.next=newNode(55);Console.WriteLine(getNode(head2,6));}}
JavaScript
'use strict';classNode{constructor(x){this.data=x;this.next=null;}}/** * @param {Node} head * @param {number} k * @return {number} */functiongetNode(head,k){letposition=1;// Traverse until the k-th node is found.while(head!==null){if(position===k){returnhead.data;}head=head.next;position++;}return-1;}// Driver Codeconsthead1=newNode(1);head1.next=newNode(2);head1.next.next=newNode(3);head1.next.next.next=newNode(4);head1.next.next.next.next=newNode(5);head1.next.next.next.next.next=newNode(6);head1.next.next.next.next.next.next=newNode(7);console.log(getNode(head1,3));consthead2=newNode(19);head2.next=newNode(28);head2.next.next=newNode(37);head2.next.next.next=newNode(46);head2.next.next.next.next=newNode(55);console.log(getNode(head2,6));