by Dinesh Thakur

 

import java.io.*;
class node
{
         public int v;
         public node nxt;
         public node(int x)
          {
             v=x;
          }
             public void dispval()
               {
                   System.out.println(v);
                }
}
   class LinkList
   {
           private node first,p,q;
           public LinkList()
             {
               first=null;
             }
                 public void insertval(int x)
                      {
                           node p=new node(x);
                           p.nxt=null;
                           if(first==null)
                               {
                                  first=p;
                                  q=p;
                               }
                                  else
                                     {
                                        q.nxt=p;
                                        q=p;
                                      }
                     }
                       public void dellast()
                       {
                          p=first;
                          q=first.nxt;
                          while(q.nxt !=null)
                                {
                                     p=q;
                                     q=q.nxt;
                                }
                                     p.nxt=null;
                        }
                         public void displayList()
                                 {
                                     node r= first;
                                     while(r !=null)
                                             {
                                                  r.dispval();
                                                  r=r.nxt;
                                             }
                                 }
    }
     class DeletingLastElement
    {
            public static void main(String args[]) throws IOException
             {
                   LinkList k=new LinkList();
                   BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
                   String h;
                   int n,i,m;
                   System.out.println("How many numbers you want to insert in Linked list");
                   h=b.readLine();
                   n=Integer.parseInt(h);
                   System.out.println("Enter numericals in linked list");
                   for(i=1;i<=n;i++)
                         {
                               h=b.readLine();
                               m=Integer.parseInt(h);
                                k.insertval(m);
                         }
                               System.out.println("Data in linked list is");
                               k.displayList();                            
                               k.dellast();
                               System.out.println("Data in linked list after deleting last element is");
                               k.displayList();
            }
    }                                                                              

Deleting Last Element of Linked List