-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReversedLinkedList.java
46 lines (40 loc) · 1.09 KB
/
ReversedLinkedList.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
public class ReversedLinkedList {
private Node revFirst;
public static class Node {
private int val;
private Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
public int getVal() {
return this.val;
}
}
public ReversedLinkedList(Node first) {
if (first == null) {
revFirst = null;
} else {
Node currentNode = first;
Node next = currentNode.next;
Node newNext = null;
while (next != null) {
currentNode.next = newNext;
newNext = currentNode;
currentNode = next;
next = currentNode.next;
}
currentNode.next = newNext;
revFirst = currentNode;
}
}
public Node getRevFirst() {
return revFirst;
}
public static void main(String[] args) {
Node first = new Node(12, new Node(24, new Node(36, null)));
ReversedLinkedList testcase = new ReversedLinkedList(first);
Node revFirst = testcase.getRevFirst();
System.out.println("reversed first value: " + revFirst.getVal());
}
}