-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseDLL.java
More file actions
66 lines (66 loc) · 1.25 KB
/
reverseDLL.java
File metadata and controls
66 lines (66 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
public class Main
{
public static class Node{
int data;
Node prev;
Node next;
Node(int data)
{
this.data=data;
prev=next=null;
}
}
static Node head;
static void insert(int data)
{
Node n=new Node(data);
if(head==null)
{
head=n;
return;
}
else
{
Node ptr=head;
while(ptr.next!=null)
{
ptr=ptr.next;
}
n.prev=ptr;
ptr.next=n;
}
}
static void reverse()
{
if(head==null ||head.next==null)
return;
Node t=null;
Node curr=head;
while(curr!=null)
{
t=curr.prev;
curr.prev=curr.next;
curr.next=t;
curr=curr.prev;
}
head=t.prev;
}
public static void main(String[] args) {
System.out.println("Enter the size");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=0;i<n;i++)
{
insert(s.nextInt());
}
reverse();
Node ptr=head;
System.out.println("After reverse");
while(ptr!=null)
{
System.out.println(ptr.data);
ptr=ptr.next;
}
}
}