You are on page 1of 2

import java.util.

Scanner;
class Node
{
int data;
Node next;
public Node(int data)
{
this.data=data;
next=null;
}
}
class Linkedlist
{
Node head;
void append(int data)
{
Node newNode=new Node(data);
Node current=head;
if(head==null)
{
head=newNode;
return;
}
else
{
while(current.next!=null)
{
current=current.next;
}
current.next=newNode;
return;
}
}
public void display()
{
Node temp=head;
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 List
{
public static void main(String[] args)
{
Linkedlist b=new Linkedlist();
Scanner sc=new Scanner(System.in);
String ch;
do
{
System.out.println("Enter the value:");
int val=sc.nextInt();
b.append(val);
System.out.println("Do you want to add another node? Type Yes/No");
ch=sc.next();
}while(ch.equals("Yes")||(ch.equals("yes")));
System.out.println("The elements in the linked list are:");
b.display();
b.Reverse();
System.out.println("\nThe elements in the reversed linked list are:");
b.display();
}
}

You might also like