Find kth node from end of the linked list in JAVA
JAVA Program to find kth node from the end
In this page of Data Structures we’ll take a look at the solution of a problem where it has been asked to find the kth node from the end of the Linked List in JAVA. Here we’ve used a rather simpler solution of finding out the index of the element by basic mathematics implementation using the length of the Linked List.
Implementation to find Kth node from end of the Linked List in JAVA
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.
Then we’ll find out the index of that kth node from the beginning of the list by subtracting its position, from the end, from the length of the linked list.
We can solve this problem in one traversal only. The idea is to start from the head node to move a pointer K nodes ahead in given list.
Algorithm
- printk(k)
- Node temp = head
- FOR int i=0; i++ Until temp!=0
- temp = temp->next;
- length++
- temp = head
- WHILE i = 1 to i < length – k + 1
- temp = temp->next
- PRINT temp->data
Code in JAVA Programming Language
import java.lang.*; class LinkedList { Node head; // not using parameterized constructor would by default // force head instance to become null // Node head = null; // can also do this, but not required // Node Class class Node { int data; Node next; Node (int x) // parameterized constructor { data = x; next = null; } } public Node insertStart (int data) { // Creating newNode memory & assigning data value Node newNode = new Node (data); // assigning this newNode's next as current head node newNode.next = head; // re-assigning head to this newNode head = newNode; return head; } public void insertLast (int data) { // Creating newNode memory & assigning data value Node newNode = new Node (data); // if Linked List was empty, entering first node if (head == null) { head = newNode; return; } // required to traverse the Linked List Node temp = head; // traverse till the last node in Linked List while (temp.next != null) temp = temp.next; // assigning the current last node's next to this newNode // thus newNode becomes the last node in Linked List temp.next = newNode; } public void insertPosition (int n, int data) { int size = calcSize (head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if (n < 1 || n > size) { System.out.println ("Can't insert\n"); } else { Node newNode = new Node (data); // required to traverse Node temp = head; // traverse to the nth node while (--n > 0) temp = temp.next; // assigning this newNode's next to nth node's next newNode.next = temp.next; // assigning the nth node's next to this newNode temp.next = newNode; // newNode added after nth node } } void printk (int k) { int length = 0; Node temp = head; for (int i = 0; temp != null; i++) { temp = temp.next; length++; } temp = head; //to get kth node from the end we can also say that we want (length-k+1) node from the beginning int i = 1; while (i < length - k + 1) { temp = temp.next; i++; } System.out.println (k + "th Node from the end is " + temp.data); } public void display () { Node node = head; //as linked list will end when Node reaches Null while (node != null) { System.out.print (node.data + " "); node = node.next; } System.out.println (); } // required for insertPosition() method public int calcSize (Node node) { int size = 0; // traverse to the last node each time incrementing the size while (node != null) { node = node.next; size++; } return size; } } public class Main { public static void main (String args[]) { LinkedList ll = new LinkedList (); ll.insertStart (3); ll.insertStart (2); ll.insertStart (1); ll.insertLast (5); ll.insertLast (6); ll.insertLast (7); ll.insertLast (8); ll.display (); ll.printk(4); } }
Output
1 2 3 5 6 7 8 4th Node from the end is 5
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