Skip to content

Commit 07af2fb

Browse files
committed
Update index.ts
1 parent 0325e66 commit 07af2fb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

reverse-linked-list/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ export class ListNode {
2222
export default function reverseLinkedList(
2323
head: ListNode | null,
2424
): ListNode | null {
25-
if (null === head) {
25+
let ans = null;
26+
let x = head;
27+
while (x != null) {
28+
ans = new ListNode(x.val, ans);
29+
x = x.next;
30+
}
31+
return ans;
32+
/* if (null === head) {
2633
return null;
2734
}
2835
29-
return ArrayToListNode(Array.from(ListNodeToArray(head)).reverse());
36+
return ArrayToListNode(Array.from(ListNodeToArray(head)).reverse()); */
3037
}
3138

3239
export function ArrayToListNode(array: Array<number>): ListNode | null {

0 commit comments

Comments
 (0)