C++ Program For Writing A Function To Get Nth Node In A Linked List Last Updated : 12 Oct, 2023 Comments Improve Suggest changes Like Article Like Report Write a C++ Program to 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 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Algorithm: 1. Initialize count = 02. Loop through the linkList 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 <bits/stdc++.h> using namespace std; // Link list node class Node { public: int data; 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(Node** head_ref, int new_data) { // Allocate node Node* new_node = new Node(); // Put in the data new_node->data = new_data; // Link the old list // of 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(Node* head, int index) { 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 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 cout << "Element at index 3 is " << GetNth(head, 3); return 0; } // This code is contributed by rathbhupendra OutputElement at index 3 is 4Time Complexity: O(n)Space complexity: O(1) using only constant variables C++ Program For Writing A Function To Get Nth Node In A Linked List using Recursion: Algorithm: getnth(node,n)1. Initialize count = 02. if count==n return node->data3. else return getnth(node->next,n-1)Implementation: C++ // C++ program to find n'th node in // linked list using recursion #include <bits/stdc++.h> using namespace std; // 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 of 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. (Don't use another variable)*/ int GetNth(struct Node* head, int n) { // If length of the list is less // than the given index, return -1 if (head == NULL) return -1; // If n equal to 0 return node->data if (n == 0) return head->data; // Increase head to next pointer // n - 1: decrease the number of // recursions until n = 0 return GetNth(head->next, n - 1); } // 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(); } OutputElement at index 3 is 4Time Complexity: O(n) Space Complexity: O(n) since using constant space to create nodes. 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 C++ Program For Writing A Function To Get Nth Node In A Linked List kartik Follow Improve Article Tags : Linked List C++ Programs C++ DSA GetNth Linked Lists +2 More Practice Tags : CPPLinked List Similar Reads C++ Program For Writing A Function To Delete A Linked List Algorithm For C++:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C++ // C++ program to delete a linked list #include <bits/stdc++.h> using namespace std 2 min read C++ Program For Inserting A Node In A Linked List Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the listTable of ContentInsert a Node at the 9 min read C++ 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 C++ Program for Deleting a Node in a Linked List Write a C++ program to delete a node from the given link list.ExamplesInput: Linked List: 10 -> 20 -> 30 -> 40 -> 50, Position to delete: 3Output: 10 -> 20 -> 40 -> 50Explanation: The node at position 3 is removed. The list then connects node 20 directly to node 40.Input: Linked 6 min read C++ Program For Rearranging A Given Linked List In-Place Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 - 7 min read C++ 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 list and n = 3, then output is "B" Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Use length 4 min read C++ 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 C++ 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 = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked L 3 min read C++ 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 C++ Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves 3 min read Like