We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f9f21fd commit 6e0eb8eCopy full SHA for 6e0eb8e
reverse-linked-list/chjung99.java
@@ -0,0 +1,39 @@
1
+/**
2
+ * time: O(n)
3
+ * space: O(1)
4
+ */
5
+
6
7
+ * Definition for singly-linked list.
8
+ * public class ListNode {
9
+ * int val;
10
+ * ListNode next;
11
+ * ListNode() {}
12
+ * ListNode(int val) { this.val = val; }
13
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
14
+ * }
15
16
+class Solution {
17
+ ListNode tail;
18
+ public ListNode reverseList(ListNode head) {
19
+ reverseNode(head);
20
+ return tail;
21
+ }
22
23
+ public void reverseNode(ListNode node){
24
+ if (node == null) return;
25
+ if (node.next == null) {
26
+ tail = node;
27
+ return;
28
29
30
+ ListNode nextNode = node.next;
31
+ node.next = null;
32
33
+ reverseNode(nextNode);
34
35
+ nextNode.next = node;
36
37
38
+}
39
0 commit comments