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

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

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.

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

Syntax

class Node  

    int data; 
    Node *next; 
}; 
C++ program to find kth node from end of the linked list – 1

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • 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
  • 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 –
    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

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