C Program For Writing A Function To Get Nth Node In A Linked List Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes 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: C // C program to find n'th // node in linked list #include <assert.h> #include <stdio.h> #include <stdlib.h> // Link list node struct Node { int data; struct Node* next; }; /* 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. */ void push(struct Node** head_ref, int new_data) { // Allocate node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // Put in the data new_node->data = new_data; // Link the old list // off the new node new_node->next = (*head_ref); // Move the head to point // to the new node (*head_ref) = new_node; } // Takes head pointer of // the linked list and index // as arguments and return // data at index int GetNth(struct Node* head, int index) { struct Node* current = head; // The index of the node we're // 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(0); } // Driver Code int main() { // Start with the empty list struct Node* head = NULL; // Use push() to construct list // 1->12->1->4->1 push(&head, 1); push(&head, 4); push(&head, 1); push(&head, 12); push(&head, 1); // Check the count function printf("Element at index 3 is %d", GetNth(head, 3)); getchar(); } Output: 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! Create Quiz Comment K kartik Follow 1 Improve K kartik Follow 1 Improve Article Tags : C Language GetNth Linked Lists Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like