Find kth node from end of the linked list in C

To find kth node from end of the linked list

Find kth Node from end of the linked list in C ,We know that in singly linked list traversal is done through front only. so we will count the element from front to end of the linked list and we will find the position from front to last. when we find the position of the element then we get the node.

We can solve this problem in one traversal only.The idea is start from the head node to move a pointer K nodes ahead  in given list.

Find kth node from end of the linked list in C

Working for finding the kth node:-

  1. First take the input in the list.
  2. Initially PTR -> HEAD (pointer is the head of the linked list).
  3. Then PTR=NULL (we need to find kth from the end of the linked list).
  4. When k=3 means the last third element of the linked list.
  5. After that print it.
find-kth-node-from-the-end-in-A-Singly-Linked-List

Structure of the node in the Singly Linked List:-

struct node
{
int data;
struct node *next;
};

Find kth Node from end of the linked list in C:-

Run
#include<stdio.h>  		//header files
#include<stdlib.h>  		//library files

 // Data Structure to store a linked list node
struct Node
{
  int data;
  struct Node *next;
};

      // Iterative function to return K'th node from the end in a linked list
struct Node *getKthFromtheEnd (struct Node *head, int k)
{
  struct Node *temp = head;
  int n = 0;


// Count number of nodes in the linked list
  while (temp)
    {
      temp = temp->next;
      n++;
    }

// if number of nodes is more than or equal to K
  if (n >= k)
    {
// return (n-k+1)th node from the beginning
      temp = head;
      for (int i = 0; i < n - k; i++) temp = temp->next;
    }

   return temp;
}

// function for create a new Node with the given data and
// pushes it onto the front of the list

void push (struct Node **head, int data)
{
// create a new linked list node from heap
  struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

  newNode->data = data;
  newNode->next = *head;
  *head = newNode;
}

// main method
int main (void)
{
// given input keys
  int keys[] = { 1, 2, 3, 4, 5 };
  int n = sizeof (keys) / sizeof (keys[0]);

  struct Node *head = NULL;
  for (int i = n - 1; i >= 0; i--)
    push (&head, keys[i]);

  int k = 3;
  struct Node *node = getKthFromtheEnd (head, k);

  if (node)
    printf ("K'th node from the end is %d", node->data);

  return 0;
}
Output:-
K'th node from the end is 3

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

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