From 4f1433f802845e6371655a820c593b3b5f27775e Mon Sep 17 00:00:00 2001 From: justin Date: Sun, 11 Dec 2022 23:52:17 +0800 Subject: [PATCH] Add the module TypeScript code (Chapter of Linkedlist) --- codes/typescript/module/ListNode.ts | 9 +++++++++ codes/typescript/module/PrintUtil.ts | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 codes/typescript/module/ListNode.ts create mode 100644 codes/typescript/module/PrintUtil.ts diff --git a/codes/typescript/module/ListNode.ts b/codes/typescript/module/ListNode.ts new file mode 100644 index 0000000..543f1cf --- /dev/null +++ b/codes/typescript/module/ListNode.ts @@ -0,0 +1,9 @@ +// Definition for singly-linked list. +export default 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; + } +} diff --git a/codes/typescript/module/PrintUtil.ts b/codes/typescript/module/PrintUtil.ts new file mode 100644 index 0000000..c2e3f3c --- /dev/null +++ b/codes/typescript/module/PrintUtil.ts @@ -0,0 +1,18 @@ +/* + * File: PrintUtil.ts + * Created Time: 2022-12-10 + * Author: Justin (xiefahit@gmail.com) + */ + +import ListNode from './ListNode'; + +function printLinkedList(head: ListNode | null): void { + const list: string[] = []; + while (head !== null) { + list.push(head.val.toString()); + head = head.next; + } + console.log(list.join(' -> ')); +} + +export { printLinkedList };