Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ReverseLinkedList #880

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Java/Data-Structures/ReverseLinkedList
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.*;
class node {
static Scanner sc=new Scanner(System.in);
int data;
node next;
node(int n)
{
data=n;
next=null;
}
static node takeInput(){
int d;
System.out.println("Enter the Linked List Terminated with -1");
d=sc.nextInt();
node head=null;
node tail=null;
while(d!=-1)
{
node m=new node(d);
if(head==null)
{
head=m;
tail=m;
}
else
{
tail.next=m;
tail=tail.next;
}
d=sc.nextInt();
}
return head;
}
static void print(node head)
{
node temp=head;
while(temp!=null)
{
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.print("null");
}
static node reverse(node head)
{

node prev=null;
node curr=head;
node next;
while(curr!=null){
next=curr.next;

curr.next=prev;
prev=curr;
curr=next;
}
//head = prev;
return prev;
}



public static void main(String[] args) {

node head=takeInput();
head=reverse(head);
System.out.println("Reversed Linked list is ");
print(head);
}
}