added: linear search for Typescript

This commit is contained in:
danielsss
2023-01-07 16:38:13 +11:00
parent eb621df7a7
commit e1d561bc08
2 changed files with 62 additions and 0 deletions
+15
View File
@@ -14,4 +14,19 @@ export default class ListNode {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
/**
* Generate a linked-list with an array
* @param arr
* @return ListNode
*/
public static arrToLinkedList(arr: number[]): ListNode | null {
const dum = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
}