Update TypeScript style (Chapter of Array and Linkedlist)

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