C 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.
Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30
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.
Element at index 3 is 4
Time Complexity: O(n)
Space Complexity : O(1) since using constant space for nodes and variables.
Please refer complete article on Write a function to get Nth node in a Linked List for more details!