Update TypeScript codes.

This commit is contained in:
Yudong Jin
2023-02-02 03:08:43 +08:00
parent 7d14c9440e
commit 29dbe8cd82
21 changed files with 94 additions and 62 deletions
+18 -16
View File
@@ -7,26 +7,28 @@
/**
* Definition for a singly-linked list node
*/
export default class ListNode {
class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
/**
* Generate a linked list with an array
* @param arr
* @return
*/
arrToLinkedList(arr: number[]): ListNode | null {
const dum: ListNode = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
}
/**
* Generate a linked list with an array
* @param arr
* @return
*/
function arrToLinkedList(arr: number[]): ListNode | null {
const dum: ListNode = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
export { ListNode, arrToLinkedList };
+1 -1
View File
@@ -4,7 +4,7 @@
* Author: Justin (xiefahit@gmail.com)
*/
import ListNode from './ListNode';
import { ListNode } from './ListNode';
import { TreeNode } from './TreeNode';
/**