|
|
@ -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; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
## 使用链表实现队列 |
|
|
|
## 使用链表实现队列 |