document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Created by MOHIT on 24-01-2018.
  5.  */
  6.  
  7. class node{
  8.  
  9.     public int element;
  10.     public node link;
  11.  
  12.     //constructor that accepts only element
  13.     node(int element) {
  14.         this.element = element;
  15.         this.link = null;
  16.     }
  17.  
  18.     //constructor that accepts both link and element
  19.     node(int element, node link){
  20.         this.element = element;
  21.         this.link = link;
  22.     }
  23.  
  24.     //method to update the element
  25.     void updateData(int element){
  26.         this.element = element;
  27.     }
  28.  
  29.     //method to update or setup link
  30.     void updateLink(node link){
  31.         this.link = link;
  32.     }
  33.  
  34.     //method to get the element from the node
  35.     int getElement(){
  36.         return this.element;
  37.     }
  38.  
  39.     //method to get the next node
  40.     node getNextNode(){
  41.         return this.link;
  42.     }
  43. }
  44.  
  45. class LL {
  46.  
  47.     Scanner sc = new Scanner(System.in);
  48.     node head;
  49.  
  50.     void insertAtStart(int number){
  51.  
  52.         node newNode = new node(number);
  53.  
  54.         if(head == null){
  55.             head = newNode;
  56.         } else {
  57.             newNode.updateLink(head);
  58.         }
  59.         head = newNode;
  60.  
  61.         return;
  62.     }
  63.  
  64.     void insertAtEnd(int element){
  65.         if(head == null){
  66.             this.insertAtStart(element);
  67.             return;
  68.         }
  69.         node current = this.head;
  70.         while(current.getNextNode() != null){
  71.             current = current.getNextNode();
  72.         }
  73.  
  74.         node newNode = new node(element);
  75.         current.updateLink(newNode);
  76.         return;
  77.     }
  78.  
  79.     void display(){
  80.         node current = this.head;
  81.  
  82.         while(current!= null){
  83.             System.out.print(current.getElement());
  84.             if(current.getNextNode() != null){
  85.                 System.out.print("-->");
  86.             }
  87.             current = current.getNextNode();
  88.         }
  89.     }
  90.  
  91.     // deleting node with only that node pointer
  92.     public void deleteNode(node current){
  93.         if(current == null){
  94.             return;
  95.         }else{
  96.             // make a pointer to next node
  97.             node nextNode = current.getNextNode();
  98.  
  99.             // copy deteils of next node to current node
  100.             current.updateData(nextNode.getElement());
  101.             current.updateLink(nextNode.getNextNode());
  102.  
  103.             // delete next node
  104.             nextNode = null;
  105.         }
  106.     }
  107.  
  108.     // deleting kth element from the end
  109.     void delete_kth_Element_End(int k){
  110.         node first = this.head;
  111.         node second = this.head;
  112.  
  113.         for(int i=0; i < k; i++){
  114.             second = second.getNextNode();
  115.         }
  116.  
  117.         while(second != null){
  118.             first = first.getNextNode();
  119.             second = second.getNextNode();
  120.         }
  121.  
  122.         deleteNode(first);
  123.     }
  124. }
  125. public class LinkedList {
  126.     public static void main(String arg[]) {
  127.         System.out.println("Program to delete the kth element from the end of Linked List.");
  128.  
  129.         LL l1 = new LL();
  130.         l1.insertAtEnd(1);
  131.         l1.insertAtEnd(2);
  132.         l1.insertAtEnd(3);
  133.         l1.insertAtEnd(4);
  134.         l1.insertAtEnd(5);
  135.         l1.insertAtEnd(6);
  136.         l1.insertAtEnd(7);
  137.         l1.insertAtEnd(8);
  138.         l1.display();
  139.  
  140.         System.out.println("\\nThe linked list after deleting the 3rd node from the end is : ");
  141.         l1.delete_kth_Element_End(3);
  142.         l1.display();
  143.     }
  144. }
');