Java Code:
public class LL
{
Node head;
class Node
{
String data;
Node next;
Node(String data)
{
this.data=data;
this.next=null;
}
}
public void addFirst(String data)
{
Node newNode = new Node(data);
if(head==null)
{
head=newNode;
return;
}
newNode.next = head;
head=newNode;
}
public void addLast(String data)
{
Node newNode = new Node(data);
if(head==null)
{
head=newNode;
return;
}
Node currNode = head;
while(currNode.next!=null)
{
currNode=currNode.next;
}
currNode.next=newNode;
}
public void printLL()
{
if(head==null)
{
System.out.println(“empty LL”);
return;
}
Node currNode = head;
while(currNode!=null)
{
System.out.print(currNode.data+”–>”);
currNode=currNode.next;
}
System.out.println(“NULL”);
}
public void DeleteFirst()
{
if(head==null)
{
System.out.println(“empty list”);
return;
}
head=head.next;
}
public void DeleteLast()
{
if(head==null)
{
System.out.println(“empty list”);
return;
}
if(head.next==null)
{
head=null;
return;
}
Node seclstNode = head;
Node lastNode = head.next;
while(lastNode.next!=null)
{
lastNode = lastNode.next;
seclstNode = seclstNode.next;
}
seclstNode.next=null;
}
public static void main(String[] args)
{
LL obj = new LL();
obj.addFirst(“World”);
obj.addFirst(“Hello”);
obj.addLast(“Java”);
obj.printLL();
obj.DeleteFirst();
obj.printLL();
obj.DeleteLast();
obj.printLL();
}
}