Java Program For Writing A Function To Get Nth Node In A Linked List
Last Updated :
17 Aug, 2023
Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position.
Example:
Input: 1->10->30->14, index = 2
Output: 30
The node at index 2 is 30
Algorithm:
1. Initialize count = 0
2. Loop through the link list
a. If count is equal to the passed index then return current
node
b. Increment count
c. change current to point to next of the current.
Implementation:
Java
// Java program to find n'th node
// in linked list
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
class LinkedList
{
// Head of list
Node head;
// Takes index as argument and
// return data at index
public int GetNth(int index)
{
Node current = head;
// Index of Node we are
// currently looking at
int count = 0;
while (current != null)
{
if (count == index)
return current.data;
count++;
current = current.next;
}
/* If we get to this line, the caller
was asking for a non-existent element
so we assert fail */
assert (false);
return 0;
}
/* Given a reference to the head of a list
and an int, inserts a new Node on the
front of the list. */
public void push(int new_data)
{
// 1. Alloc the Node and put data
Node new_Node = new Node(new_data);
// 2. Make next of new Node as head
new_Node.next = head;
// 3. Move the head to point to new Node
head = new_Node;
}
// Driver code
public static void main(String[] args)
{
// Start with empty list
LinkedList llist = new LinkedList();
// Use push() to construct list
// 1->12->1->4->1
llist.push(1);
llist.push(4);
llist.push(1);
llist.push(12);
llist.push(1);
// Check the count function
System.out.println("Element at index 3 is " +
llist.GetNth(3));
}
}
Output:
Element at index 3 is 4
Time Complexity: O(n)
Space Complexity: O(1) since using constant space to create nodes and variables.
Method 2- With Recursion:
Algorithm:
getnth(node,n)
1. Initialize count = 0
2. if count==n
return node->data
3. else
return getnth(node->next,n-1)
Implementation:
Java
// Java program to find n'th node
// in linked list using recursion
class GFG
{
// Link list node
static class Node
{
int data;
Node next;
Node(int data)
{
this.data = data;
}
}
/* Given a reference (pointer to pointer)
to the head of a list and an int, push
a new node on the front of the list. */
static Node push(Node head, int new_data)
{
// Allocate node
Node new_node = new Node(new_data);
// Put in the data
new_node.data = new_data;
new_node.next = head;
head = new_node;
return head;
}
/* Takes head pointer of the linked list
and index as arguments and return data
at index*/
static int GetNth(Node head, int n)
{
int count = 0;
// Edge case - if head is null
if (head == null)
return -1;
// If count equal too n return
// node.data
if (count == n)
return head.data;
// Recursively decrease n and
// increase head to next pointer
return GetNth(head.next, n - 1);
}
// Driver code
public static void main(String args[])
{
// Start with the empty list
Node head = null;
// Use push() to construct list
// 1.12.1.4.1
head = push(head, 1);
head = push(head, 4);
head = push(head, 1);
head = push(head, 12);
head = push(head, 1);
// Check the count function
System.out.printf("Element at index 3 is %d",
GetNth(head, 3));
}
}
// This code is contributed by Arnab Kundu
Output:
Element at index 3 is 4
Time Complexity: O(n)
Space complexity: O(n) for call stack
Please refer complete article on Write a function to get Nth node in a Linked List for more details!
Similar Reads
Javascript Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1 -> 10 -> 30 -> 14, index = 2Output: 30 Explanation: The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, befo
4 min read
Write a function to get Nth node in a Linked List 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 = 2Output: 10Explanation: The node value at index 2 is 10 Input: 1->32->12
11 min read
Javascript Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node
7 min read
Program for Nth node from the end of a Linked List Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1.Examples:Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list.Input: 35 ->
14 min read
Javascript Program To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples:Input:M = 2, N = 2Linked List: 1->2->3->4->5->6->7->8Output:Linked List:
3 min read
Javascript Program for Clockwise rotation of Linked List Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, K = 2 Output: 4 -> 5 -> 1 -> 2 -> 3 -> NULLInput: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL
4 min read