C++ program to find kth node from end of the linked list

Fint kth node from end in C++

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

S

T

E

P

S

  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.

Following the above steps will help us in finding kth node from end of the linked list

S

T

E

P

S

  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.

Following the above steps will help us in finding kth node from end of the linked list

How we can construct a linked list?

Syntax

class Node  

    int data; 
    Node *next; 
}; 

Using following set of codes in our program we can construct linked list.

C++ program to fint kth node from end

Algorithm to find kth node from end of the linked list

A

L

G

O

R

I

T

H

M

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

A

L

G

O

R

I

T

H

M

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

#include <iostream>
using namespace std;

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

void make(int n);                 
struct node* getkthLastNode(struct node *stnode,int k, int n);

void print();

 
int main()
{
    int n,num,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;
    getkthLastNode(stnode,k,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;      
        stnode-> nextptr = NULL; //Links the address field to NULL
        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 "<<i<<": "; // Entering data in nodes.
                cin>>num;
                frntNode->num = num;         
                frntNode->nextptr = NULL;    
                tmp->nextptr = frntNode;     
                tmp = tmp->nextptr;
            }
        }
    }
} 
void print()//function to display linked list
{
    struct node *tmp;
    if(stnode == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        tmp = stnode;
        cout<<"Linked List\t";
        while(tmp != NULL)
        {
            cout<<tmp->num<<"\t";   
            tmp = tmp->nextptr;                
        }
    }
}

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 NULL;
    }
    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;
    }

    cout<< back->num;//printing data at middle address
}
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

Learn to code similar program in C programming language