# 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

1. Construct nodes and initialize the variables.
2. Now construct some functions to create linked list and to display linked list.
3. Now create function to get kth node from the last.
4. In this function define two pointers named front and back.
5. Traverse front pointer till k-1 node.
6. Now traverse both pointer front and back till front points to NULL
7. As the front reaches the end of list back will be at last kth node.
8. Print result.

### 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:-

1. IF K>LIST SIZE
2. RETURN NULL
3. FOR I=0 TO K-1
4. FRONT=FRONT->NEXTPTR
5. WHILE(FRONT->NEXTPTR!=NULL)
6. FRONT=FRONT->NEXTPTR
7. BACK=BACK->NEXTPTR
8. RETURN BACK->NUM

## Program to delete alternate nodes of a linked list in C++

Run
```#include<iostream>
#include<bits/stdc++.h>
using namespace std;

struct node
{
int num;
node *nextptr;
}*stnode; //node constructer

// ------------------------------------------
// void make(int n);
void make(int n) //function to create linked list.
{
struct node *frntNode, *tmp;
int num, i;

stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
cout<<"Memory can not be allocated";
}
else
{

cout<<"Enter the data for node 1: ";
cin>>num;
stnode-> num = num;
tmp = stnode;

for(i=2; i<=n; i++)
{
frntNode = (struct node *)malloc(sizeof(struct node));

if(frntNode == NULL) //If frntnode is null no memory cannot be allotted
{
cout<<"Memory can not be allocated";
break;
}
else
{
cout<<"Enter the data for node "<>num;
frntNode->num = num;
frntNode->nextptr = NULL;
tmp->nextptr = frntNode;
tmp = tmp->nextptr;
}
}
}
}
// --------------

//struct node* getkthLastNode(struct node *stnode,int k, int n);
struct node getkthLastNode(struct node *stnode, int k,int n)// Function to find the Nth node from the last
{
struct node *front, *back;
int i;
front = back = stnode;
if(k > n)
{
cout << "\nk is greater than the size of Linked List\n";
// return -1;
}
for(i = 0; i < k-1; i++) { front = front->nextptr;
}
//moving both pointers together till front reaches last node of linked list
while(front->nextptr != NULL)
{
front = front->nextptr;
back = back->nextptr;
}

return *back;//printing data at middle address
//return 0;
}

//---------------------------------------

//void print();
void print()//function to display linked list
{
struct node *tmp;
if(stnode == NULL)
{
cout<<"List is empty";
}
else
{
tmp = stnode;
while(tmp != NULL)
{
cout<num<<"\t";
tmp = tmp->nextptr;
}
}
}

int main()
{
int n,num,k;

cout<<"Enter the number of nodes: ";
cin>>n;
make(n);
print();
cout<<"\nEnter the value of k:";
cin>>k;
cout<< endl;
getkthLastNode(stnode,k,n);
}
```
```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	32	65	34	25	85
Enter the value of k:3

34```

### 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

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Linked List in Data Structure
C | C++ | Java
• Singly Linked List in –
C | C++ | Java
• Insertion in singly Linked List –
C | C++ | Java
• Insertion at beginning in singly Linked List  –
C | C++Java
• Insertion at nth position in singly Linked List  –
C | C++Java
• Insertion at end in singly Linked List  –
C | C++Java
• Deletion in singly Linked List  –
C | C++Java
• Deletion from beginning in singly linked list :
C | C++ | Java
• Deletion from nth position in singly linked list :
C | C++ | Java
• Deletion from end in singly linked list :
C | C++ | Java
• Linked List Insertion and Deletion –
C | C++Java
• Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++Java
C | C++Java
• Print reverse of a linked list without actually reversing –
C |C++Java
• Print reverse of a linked list without actually reversing –
C |C++Java
• Insertion in the middle Singly 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 given Position –
C | C++Java
• Deletion at given Position –
C | C++Java

• 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
• Insertion at beginning in singly Linked List  – C | C++Java
• Insertion at nth position in singly Linked List  – C | C++Java
• Insertion at end in singly Linked List  – C | C++Java
• Deletion in singly Linked List  – C | C++Java
• Deletion from beginning in singly linked list : C | C++ | Java
• Deletion from nth position in singly linked list : C | C++ | Java
• Deletion from end 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