Skip to content

Commit 025592b

Browse files
authored
Merge pull request #2536 from soobing/week7
[soobing] WEEK7 Solutions
2 parents e73127d + eadef71 commit 025592b

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

reverse-linked-list/soobing.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@ interface ListNode {
2424
next: ListNode | null;
2525
}
2626

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
2741
function reverseList(head: ListNode | null): ListNode | null {
2842
let prev: ListNode | null = null;
29-
30-
while (head) {
31-
const next = head.next;
32-
head.next = prev;
33-
prev = head;
34-
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;
3549
}
36-
3750
return prev;
38-
}
51+
};

0 commit comments

Comments
 (0)