mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
Add TypeScript code to docs (Chapter of Linked_list)
This commit is contained in:
parent
4f1433f802
commit
7c70b3c760
1 changed files with 70 additions and 6 deletions
|
@ -63,7 +63,15 @@ comments: true
|
|||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
|
||||
/* 链表结点结构体 */
|
||||
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; // 指向下一结点的引用
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
@ -152,7 +160,18 @@ comments: true
|
|||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个结点
|
||||
const n0 = new ListNode(1);
|
||||
const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// 构建引用指向
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
@ -251,7 +270,22 @@ comments: true
|
|||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
|
||||
/* 在链表的结点 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;
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
@ -325,7 +359,16 @@ comments: true
|
|||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
function access(head: ListNode | null, index: number): ListNode | null {
|
||||
for (let i = 0; i < index; i++) {
|
||||
if (!head) {
|
||||
return null;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
@ -407,7 +450,18 @@ comments: true
|
|||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
function find(head: ListNode | null, target: number): number {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
@ -480,7 +534,17 @@ comments: true
|
|||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
|
||||
/* 双向链表结点类 */
|
||||
class ListNode {
|
||||
val: number;
|
||||
next: ListNode | null;
|
||||
prev: ListNode | null;
|
||||
constructor(val?: number, next?: ListNode | null) {
|
||||
this.val = val === undefined ? 0 : val; // 结点值
|
||||
this.next = next === undefined ? null : next; // 指向后继结点的引用
|
||||
this.prev = prev === undefined ? null : prev; // 指向前驱结点的引用
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
|
Loading…
Reference in a new issue