Java Program For Writing A Function To Get Nth Node In A Linked List Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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 30Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. 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! Comment More infoAdvertise with us Next Article Java Program For Writing A Function To Get Nth Node In A Linked List kartik Follow Improve Article Tags : Linked List Java Java Programs DSA GetNth Linked Lists +2 More Practice Tags : JavaLinked List Similar Reads Java 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. Java // Linked List Class class LinkedList { // Head of list Node 7 min read Java Program For Segregating Even And Odd Nodes In A Linked List Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NUL 6 min read Java Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2-> 5 min read Java Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Java Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Java Program For Printing Nth Node From The End Of A Linked List Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below the list and n = 3, then the output is "B". Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1 (Use 5 min read Java Program to Create a Singly Linked List and Count the Number of Nodes Linked List is a linear data structure. Linked list elements are not stored at a contiguous location, the elements are linked using pointers. Singly Linked list is the collection of nodes, where each node has two parts one is the data and other is the linked part. Example: Input : AddNodes = {2, 3, 3 min read Java Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Java Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal 6 min read Java Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer Given singly linked list with every node having an additional "arbitrary" pointer that currently points to NULL. Need to make the "arbitrary" pointer point to the next higher value node. We strongly recommend to minimize your browser and try this yourself first A Simple Solution is to traverse all n 4 min read Like