-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
49 lines (43 loc) · 908 Bytes
/
Node.java
File metadata and controls
49 lines (43 loc) · 908 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
class Node {
Node next;
int data;
int length;
Node() {
data = 0;
length = 0;
next = null;
}
Node(int d) {
data = d;
next = null;
length = 1;
}
void append(int d) {
Node newNode = new Node(d);
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = newNode;
length++;
}
void display() {
System.out.print(data + "->");
if (next != null) {
next.display();
} else System.out.print("null\n");
}
public Node reverse() {
Node n = this;
Node prev = null;
Node current = n;
while (current != null) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
n = prev;
return n;
}
}