File size: 1,213 Bytes
c574d3a |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
class Reverse{
static Node first, last, t;
static class Node{
int data;
Node next;
Node(int d){
this.data = d;
next = null;
}
}
void create(int a[], int n){
first = new Node(a[0]);
last = first;
for(int i=1; i<n; i++){
t = new Node(a[i]);
last.next = t;
last = t;
}
}
void reverse(Node p){
int i=0;
int a[] = new int[5];
Node q = p;
while(q!=null){
a[i] = q.data;
q = q.next;
i++;
}
q = p;
i--;
while(q!=null){
q.data = a[i];
q = q.next;
i--;
}
}
void reverse1(Node p){
Node q = null;
Node r = null;
while(p!=null){
r = q;
q = p;
p = p.next;
q.next = r;
}
first = q;
}
void rever(Node q, Node p){
if(p!=null){
rever(p,p.next);
p.next = q;
}
else{
first = q;
}
}
void Display(){
Node p = first;
while(p!=null){
System.out.print(p.data+" ");
p = p.next;
}
}
public static void main(String[] args){
Reverse l = new Reverse();
int a[] = {1,2,3,4,5};
int n =a.length;
l.create(a,n);
System.out.println();
l.rever(null,l.first);
l.Display();
}
}
|