-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListrev.java
More file actions
52 lines (52 loc) · 1.14 KB
/
Listrev.java
File metadata and controls
52 lines (52 loc) · 1.14 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
public class Listrev
{
static Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
next=null;
data=d;
}
}
Node rev(Node node)
{
Node prev=null;
Node current=node;
Node next=null;
while(current!=null)
{
next=current.next;
current.next=prev;
prev = current;
current=next;
}
node=prev;
return node;
}
void print(Node node)
{
while(node!=null)
{
System.out.print(node.data+" ");
node = node.next;
}
}
public static void main(String args[])
{
Listrev lr=new Listrev();
lr.head=new Node(10);
lr.head.next=new Node(20);
lr.head.next.next=new Node(30);
lr.head.next.next.next=new Node(40);
lr.head.next.next.next.next=new Node(50);
System.out.println("Initial linked List");
lr.print(head);
head=lr.rev(head);
System.out.println("");
System.out.println("Reversed List");
lr.print(head);
}
}