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.
2 parents e73127d + eadef71 commit 025592bCopy full SHA for 025592b
1 file changed
reverse-linked-list/soobing.ts
@@ -24,15 +24,28 @@ interface ListNode {
24
next: ListNode | null;
25
}
26
27
+// function reverseList(head: ListNode | null): ListNode | null {
28
+// let prev: ListNode | null = null;
29
+
30
+// while (head) {
31
+// const next = head.next;
32
+// head.next = prev;
33
+// prev = head;
34
+// head = next;
35
+// }
36
37
+// return prev;
38
39
40
+// 3rd tried
41
function reverseList(head: ListNode | null): ListNode | null {
42
let prev: ListNode | null = null;
-
- while (head) {
- const next = head.next;
- head.next = prev;
- prev = head;
- head = next;
43
+ let current = head;
44
+ while(current) {
45
+ const next = current?.next ?? null;
46
+ current.next = prev;
47
+ prev = current;
48
+ current = next;
49
50
return prev;
-}
51
+};
0 commit comments