C++ program to find kth node from end of the linked list
How to find kth node from end of the linked list?
Finding kth node from end means to find the node present at kth position from the end in a singly linked list. In the process to write a C++ program to find kth node from end of the linked list we need to initialize two pointers. First we will traverse till k-1 node using the first pointer and then we will again start traversing of both the pointers simultaneously until first pointer reaches end of the list. In this way second pointer will be at kth position from end.
Further this article, will teach you steps and algorithm to do the same.
Steps to write a C++ program to find kth node from end of the linked list
- Construct nodes and initialize the variables.
- Now construct some functions to create linked list and to display linked list.
- Now create function to get kth node from the last.
- In this function define two pointers named front and back.
- Traverse front pointer till k-1 node.
- Now traverse both pointer front and back till front points to NULL
- As the front reaches the end of list back will be at last kth node.
- Print result.
Following the above steps will help us in finding kth node from end of the linked list
Syntax
class Node
{
int data;
Node *next;
};
Algorithm to find kth node from end of the linked list
We can find last kth node of a linked list using following algorithm:-
- IF K>LIST SIZE
- RETURN NULL
- FOR I=0 TO K-1
- FRONT=FRONT->NEXTPTR
- WHILE(FRONT->NEXTPTR!=NULL)
- FRONT=FRONT->NEXTPTR
- BACK=BACK->NEXTPTR
- RETURN BACK->NUM
Program to delete alternate nodes of a linked list in C++
#include< cstdlib> using namespace std;struct node { int num; node *nextptr; };node* stnode = nullptr; // Initialize stnode to nullptrvoid make(int n) { node *frntNode, *tmp; int num, i; stnode = new node; if(stnode == nullptr) { cout<<"Memory can not be allocated"; return; } else { cout<<"Enter the data for node 1: "; cin>>num; stnode->num = num; stnode->nextptr = nullptr; // Initialize next pointer to nullptr tmp = stnode; for(i=2; i<=n; i++) { frntNode = new node; if(frntNode == nullptr) { cout<<"Memory can not be allocated"; break; } else { cout<<"Enter the data for node "<>num; frntNode->num = num; frntNode->nextptr = nullptr; tmp->nextptr = frntNode; tmp = tmp->nextptr; } } } }node* getkthLastNode(node* stnode, int k, int n) { node *front, *back; int i; front = back = stnode; if(k > n) { cout << "\nk is greater than the size of Linked List\n"; return nullptr; } for(i = 0; i < k-1; i++) { front = front->nextptr; } while(front->nextptr != nullptr) { front = front->nextptr; back = back->nextptr; } return back; }void print() { node *tmp; if(stnode == nullptr) { cout<<"List is empty"; } else { tmp = stnode; cout<<"Linked List\t"; while(tmp != nullptr) { cout<num<<"\t"; tmp = tmp->nextptr; } } }int main() { int n, k; cout<<"Enter the number of nodes: "; cin>>n; make(n); cout<<"\nLinked list data: \n"; print(); cout<<"\nEnter the value of k:"; cin>>k; cout<< endl; node* result = getkthLastNode(stnode, k, n); if(result != nullptr) { cout << "Kth node from the end: " << result->num << endl; } else { cout << "Invalid input for k." << endl; } // Free the allocated memory node* current = stnode; while(current != nullptr) { node* next = current->nextptr; delete current; current = next; } }
Output: Enter the number of nodes: 5 Enter the data for node 1: 32 Enter the data for node 2: 65 Enter the data for node 3: 34 Enter the data for node 4: 25 Enter the data for node 5: 85 Linked list data: Linked List 32 65 34 25 85 Enter the value of k:3 34
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
Login/Signup to comment