1
0
Fork 0
Browse Source

vault backup: 2022-05-12 16:22:01

master
YuJian 3 years ago
parent
commit
8defe9e09a
  1. 42
      数据结构与算法之美/数据结构 - 链表.md

42
数据结构与算法之美/数据结构 - 链表.md

@ -1,3 +1,6 @@ @@ -1,3 +1,6 @@
## 数组转换成链表
```typescript
@ -49,4 +52,41 @@ const reverseLinkedList = (LinkedList: LinkedList) => { @@ -49,4 +52,41 @@ const reverseLinkedList = (LinkedList: LinkedList) => {
};
```
## 使用链表实现队列
## 使用链表实现队列
```typescript
interface LinkedList {
value: number | null;
next: LinkedList | null;
}
class LinkedListQueue {
private head: LinkedList | null = null;
private tail: LinkedList | null = null;
private len: number = 0;
add(value: number) {
if (this.len === 0) {
this.head = { value, next: null };
this.tail = this.head;
this.len++;
return;
}
this.tail.next = { value, next: null };
this.tail = this.tail.next;
this.len++;
}
pop(): number {
if (!this.head || !this.tail) return;
const popValue = this.head.value;
this.head = this.head.next;
this.len--;
return popValue;
}
get length() {
return this.len;
}
}
```
Loading…
Cancel
Save