forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse.java
47 lines (40 loc) · 927 Bytes
/
Reverse.java
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
class Reverse {
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
static Node head;
Node reversed(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) {
Reverse l = new Reverse();
l.head = new Node(1);
l.head.next = new Node(2);
l.head.next.next = new Node(3);
l.head.next.next.next = new Node(4);
head = l.reversed(head);
System.out.println("Reversed");
l.print(head);
}
}