-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 138. 复制带随机指针的链表 | ||
|
||
给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。 | ||
|
||
构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。 | ||
|
||
例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。 | ||
|
||
返回复制链表的头节点。 | ||
|
||
用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示: | ||
|
||
val:一个表示 Node.val 的整数。 | ||
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。 | ||
你的代码 只 接受原链表的头节点 head 作为传入参数。 | ||
|
||
来源:力扣(LeetCode) | ||
|
||
链接:<https://leetcode-cn.com/problems/copy-list-with-random-pointer> | ||
|
||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
|
||
--- | ||
|
||
用一个 map 存 <老节点,新节点>,先复制 next,复制完再挨个找 random。 | ||
|
||
```js | ||
/** | ||
* // Definition for a Node. | ||
* function Node(val, next, random) { | ||
* this.val = val; | ||
* this.next = next; | ||
* this.random = random; | ||
* }; | ||
*/ | ||
|
||
/** | ||
* @param {Node} head | ||
* @return {Node} | ||
*/ | ||
var copyRandomList = function(head) { | ||
if (!head) return null | ||
var newHead = new Node(head.val, null, null) | ||
var current = head | ||
var newCurrent = newHead | ||
var map = new Map() | ||
map.set(head, newHead) | ||
|
||
while (current) { | ||
newCurrent.next = current.next ? new Node(current.next.val, null, null) : null | ||
newCurrent = newCurrent.next | ||
current = current.next | ||
map.set(current, newCurrent) | ||
} | ||
|
||
while (head) { | ||
if (head.random) { | ||
map.get(head).random = map.get(head.random) | ||
} | ||
head = head.next | ||
} | ||
|
||
return newHead | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 143. 重排链表 | ||
|
||
给定一个单链表 L:L0→L1→…→Ln-1→Ln , | ||
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… | ||
|
||
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 | ||
|
||
来源:力扣(LeetCode) | ||
|
||
链接:<https://leetcode-cn.com/problems/reorder-list> | ||
|
||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
|
||
--- | ||
|
||
- 找中点 | ||
- 反转 | ||
- 合并 | ||
|
||
```js | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {void} Do not return anything, modify head in-place instead. | ||
*/ | ||
var reorderList = function(head) { | ||
if (!head || !head.next || !head.next.next) return head | ||
|
||
var h = head, t = head | ||
while (t.next) { | ||
h = h.next | ||
t = t.next | ||
if (t.next) { | ||
t = t.next | ||
} | ||
} | ||
|
||
var r = h.next, prev = h | ||
h.next = null | ||
while (r) { | ||
var next = r.next | ||
r.next = prev | ||
prev = r | ||
r = next | ||
} | ||
|
||
|
||
h = head | ||
while (true) { | ||
var next = h.next | ||
var tnext = t.next | ||
h.next = t | ||
t.next = next | ||
h = next | ||
t = tnext | ||
if (h === t || h.next === t) return head | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 147. 对链表进行插入排序 | ||
|
||
插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。 | ||
每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。 | ||
|
||
插入排序算法: | ||
|
||
插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。 | ||
每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。 | ||
重复直到所有输入数据插入完为止。 | ||
|
||
来源:力扣(LeetCode) | ||
|
||
链接:<https://leetcode-cn.com/problems/insertion-sort-list> | ||
|
||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
|
||
--- | ||
|
||
要维护一个「已排序 | ||
|
||
```js | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var insertionSortList = function(head) { | ||
if (!head || !head.next) return head | ||
var dum = new ListNode(0, head) | ||
|
||
var sorted = head, current = head.next | ||
head.next = null | ||
while (current) { | ||
var next = current.next | ||
var p = dum.next, prev = dum | ||
while (p) { | ||
if (current.val > p.val) { | ||
if (p === sorted) { | ||
p.next = current | ||
current.next = null | ||
sorted = current | ||
p = null | ||
} else { | ||
p = p.next | ||
prev = prev.next | ||
} | ||
} else { | ||
prev.next = current | ||
current.next = p | ||
p = null | ||
} | ||
} | ||
current = next | ||
} | ||
return dum.next | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ Leet Code Solutions Repo | |
- 一个一次走一步,一个一次走两步 | ||
- 不要节省变量,能用就用 | ||
- 可以添加假的头结点 | ||
- 用 Map |