Skip to content

Commit 89ad7e8

Browse files
authored
feat: add description of the solution to lcof problem: No.35 (doocs#675)
面试题 35. 复杂链表的复制
1 parent 89f4da8 commit 89ad7e8

File tree

1 file changed

+14
-1
lines changed
  • lcof/面试题35. 复杂链表的复制

1 file changed

+14
-1
lines changed

lcof/面试题35. 复杂链表的复制/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
- 相比正常的克隆链表,多了一步操作,克隆随机指针。
55+
- 随机指针并不是读取时进行随机指向,而是没有规律。但可以确定的是,随机指向的对象都是现存的节点或 `null`
56+
- 而难点在于克隆随机指针,如克隆链表头节点时,随机指针指向链尾,而链尾还未创造出来。
57+
- 对此,需要先完成链表的基础克隆,再回头关注随机指针。
58+
59+
### 哈希表
60+
61+
- 使用哈希表记录所有节点, `key` 为原节点,`value` 为克隆节点。
62+
- 遍历链表,完成记录,克隆节点暂时只记录对应的 `val`
63+
- 再次遍历,完善克隆节点的 `next``random`
64+
65+
### 原地算法
66+
5467
首先,遍历链表,完成对每个旧节点的复制。
5568

5669
```bash
@@ -61,7 +74,7 @@ A -> B -> C -> D -> null
6174
A -> A1 -> B -> B1 -> C -> C1 -> D -> D1 -> null
6275
```
6376

64-
接着设置新节点的 ramdom 指针。
77+
接着设置新节点的 `random` 指针。
6578

6679
然后遍历链表,修改旧节点和新节点的指向,将旧节点指向下一个旧节点,而新节点指向下一个新节点。
6780

0 commit comments

Comments
 (0)