aboutsummaryrefslogtreecommitdiff
path: root/ReverseList.java
diff options
context:
space:
mode:
authorMichael Hunteman <michael@michaelted.xyz>2022-09-22 10:14:04 -0500
committerMichael Hunteman <michael@michaelted.xyz>2022-09-22 10:14:04 -0500
commit6522012065712fb0ece31bff9ff10b38a83b10e1 (patch)
tree743f3c13d08be9f60bad57b74e7a1fe7cc675e89 /ReverseList.java
Initial commit
Diffstat (limited to 'ReverseList.java')
-rw-r--r--ReverseList.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/ReverseList.java b/ReverseList.java
new file mode 100644
index 0000000..96e09a2
--- /dev/null
+++ b/ReverseList.java
@@ -0,0 +1,31 @@
+import java.lang.*;
+import java.util.*;
+import node.*;
+
+class ReverseList {
+ public static ListNode reverseList(ListNode head) {
+ ListNode prev = null;
+ ListNode node = head;
+ while (node != null) {
+ ListNode tmp = node.next;
+ node.next = prev;
+ prev = node;
+ node = tmp;
+ }
+ return prev;
+ }
+
+ public static void main(String[] args) {
+ ListNode fourth = new ListNode(5, null);
+ ListNode third = new ListNode(4, fourth);
+ ListNode second = new ListNode(3, third);
+ ListNode first = new ListNode(2, second);
+ ListNode head = new ListNode(1, first);
+
+ ListNode curr = reverseList(head);
+ while (curr != null) {
+ System.out.println(curr.val);
+ curr = curr.next;
+ }
+ }
+}