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 5bc4b26 commit 75361b7Copy full SHA for 75361b7
reverse-linked-list/njngwn.java
@@ -0,0 +1,27 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+// Time Complexity: O(n)
12
+// Sprace Compexity: O(1)
13
+class Solution {
14
+ public ListNode reverseList(ListNode head) {
15
+ ListNode prevNode = null;
16
+ ListNode currNode = head;
17
+
18
+ while (currNode != null) {
19
+ ListNode nextNode = currNode.next;
20
+ currNode.next = prevNode;
21
+ prevNode = currNode;
22
+ currNode = nextNode;
23
+ }
24
25
+ return prevNode;
26
27
+}
0 commit comments