-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverselink.java
More file actions
69 lines (68 loc) · 986 Bytes
/
Reverselink.java
File metadata and controls
69 lines (68 loc) · 986 Bytes
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
package practice;
import java.util.*;
public class linkrev
{
static class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
next=null;
}
}
Node head;
void insert(int data)
{
Node tem=new Node(data);
if(head==null)
{
head=tem;
return;
}
Node cur=head;
while(cur.next!=null)
{
cur=cur.next;
}
cur.next=tem;
}
void print(Node head)
{
Node cur=head;
while(cur.next!=null)
{
System.out.print(cur.data+"->");
cur=cur.next;
}
System.out.print(cur.data);
}
Node rev(Node head)
{
Node temp=null;
Node cur=head;
Node b=null;
while(cur!=null)
{
b=cur.next;
cur.next=temp;
temp=cur;
cur=b;
}
return temp;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size");
int size=sc.nextInt();
linkrev l=new linkrev();
for(int i=0;i<size;i++)
{
l.insert(sc.nextInt());
}
l.head=l.rev(l.head);
l.print(l.head);
}
}