Update TypeScript style (Chapter of Array and Linkedlist)

This commit is contained in:
justin 2022-12-12 21:12:32 +08:00
parent 6f5d00f827
commit 552a44fa94
2 changed files with 13 additions and 1 deletions

View file

@ -7,12 +7,14 @@
import ListNode from '../module/ListNode'; import ListNode from '../module/ListNode';
import { printLinkedList } from '../module/PrintUtil'; import { printLinkedList } from '../module/PrintUtil';
/* 在链表的结点 n0 之后插入结点 P */
function insert(n0: ListNode, P: ListNode): void { function insert(n0: ListNode, P: ListNode): void {
const n1 = n0.next; const n1 = n0.next;
n0.next = P; n0.next = P;
P.next = n1; P.next = n1;
} }
/* 删除链表的结点 n0 之后的首个结点 */
function remove(n0: ListNode): void { function remove(n0: ListNode): void {
if (!n0.next) { if (!n0.next) {
return; return;
@ -23,6 +25,7 @@ function remove(n0: ListNode): void {
n0.next = n1; n0.next = n1;
} }
/* 访问链表中索引为 index 的结点 */
function access(head: ListNode | null, index: number): ListNode | null { function access(head: ListNode | null, index: number): ListNode | null {
for (let i = 0; i < index; i++) { for (let i = 0; i < index; i++) {
if (!head) { if (!head) {
@ -33,6 +36,7 @@ function access(head: ListNode | null, index: number): ListNode | null {
return head; return head;
} }
/* 在链表中查找值为 target 的首个结点 */
function find(head: ListNode | null, target: number): number { function find(head: ListNode | null, target: number): number {
let index = 0; let index = 0;
while (head !== null) { while (head !== null) {

View file

@ -1,4 +1,12 @@
// Definition for singly-linked list. /*
* File: ListNode.ts
* Created Time: 2022-12-10
* Author: Justin (xiefahit@gmail.com)
*/
/**
* Definition for a singly-linked list node
*/
export default class ListNode { export default class ListNode {
val: number; val: number;
next: ListNode | null; next: ListNode | null;