You are on page 1of 2

import java.util.

Scanner;
class LinkedList
{
Node head;
class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
}
void deleteAllNode(int key)
{
Node temp=head;
while(temp!=null && temp.next!=null)
{
if(temp.next.data == key)
{
temp.next = temp.next.next;
}
else
{
temp=temp.next;
}
}
if(head !=null && head.data == key)
{
head = head.next;
}
}
public void push(int new_data)
{
Node new_node=new Node(new_data);
new_node.next=head;
head=new_node;
}
public void display()
{
Node temp=head;
System.out.print("The list after deletion: ");
while(temp!=null)
{
System.out.println(temp.data+" ");
temp=temp.next;
}
}
public void reverse()
{
Node previousNode=null;
Node nextNode;
Node currentNode=head;
while(currentNode != null)
{
nextNode=currentNode.next;
currentNode.next=previousNode;
previousNode=currentNode;
currentNode=nextNode;
}
head=previousNode;
}
}
public class DeleteMain
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
LinkedList b=new LinkedList();
int val=sc.nextInt();
int a[]=new int[val];
for(int i=0;i<val;i++)
{
a[i]=sc.nextInt();
b.push(a[i]);
}
System.out.println("Enter the element to be deleted: ");
int f=sc.nextInt();
b.deleteAllNode(f);
b.reverse();
b.display();
}
}

You might also like