From 12bea3e5ecded7e5fc7ee728a1067d47ac5002a8 Mon Sep 17 00:00:00 2001 From: YuJian Date: Thu, 12 May 2022 12:00:26 +0800 Subject: [PATCH] vault backup: 2022-05-12 12:00:25 --- .../数据结构 - 链表.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/数据结构与算法之美/数据结构 - 链表.md b/数据结构与算法之美/数据结构 - 链表.md index 5d53b31..22de760 100644 --- a/数据结构与算法之美/数据结构 - 链表.md +++ b/数据结构与算法之美/数据结构 - 链表.md @@ -26,4 +26,27 @@ const toLinkedList = (array: Number[]): LinkedList => { ## 反转单向链表 +```typescript +interface LinkedList { + value: Number | null; + next: LinkedList | null; +} + +const reverseLinkedList = (LinkedList: LinkedList) => { + let preNode: LinkedList | null = null; + let curNode: LinkedList | null = LinkedList; + let nexNode: LinkedList | null = LinkedList.next; + + while (curNode) { + curNode.next = preNode; + preNode = curNode; + if (nexNode == null) break; + curNode = nexNode; + nexNode = nexNode.next; + } + + return curNode; +}; +``` + ## 使用链表实现队列 \ No newline at end of file