|
| 1 | +# LeetCode 讀書會第 21 次聚會 2021/02/23 |
| 2 | + |
| 3 | +leetcode 讀書會通知 |
| 4 | +1. 項目: 第 21 次聚會 |
| 5 | +2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法 |
| 6 | +3. 時間: 2/23 (二) 20:00 ~ 21:00 |
| 7 | +4. 地點: google meet 線上 (前 10 分鐘預備鏈接) |
| 8 | +5. 解題項目: [Linked list - ](https://leetcode.com/explore/learn/card/linked-list/210/doubly-linked-list/) |
| 9 | + |
| 10 | + ```text |
| 11 | + 3. Medium 430. Flatten a Multilevel Doubly Linked |
| 12 | + > List Hint: Try to solve this problem recursively. |
| 13 | + 4. Medium 708. Insert into a Cyclic Sorted List |
| 14 | + > Hint: A cyclic list here is a linked list that the last node is linked with the first node and form a cycle. Find out the smallest node will help you determine the suitable position. |
| 15 | + 5. Medium 138. Copy List with Random Pointer |
| 16 | + > Hint: Using a hash set maybe is a good idea. Refer to our hash table card if you are not familiar with the hash set. |
| 17 | + ``` |
| 18 | +
|
| 19 | +6. 共筆: GitHub https://github.com/programmingbookclub/Leetcode-club |
| 20 | +
|
| 21 | +
|
| 22 | +--- |
| 23 | +
|
| 24 | +
|
| 25 | +```swift |
| 26 | +// Swift Flatten a Multilevel Doubly Linked List |
| 27 | +
|
| 28 | +
|
| 29 | +/** |
| 30 | + * Definition for a Node. |
| 31 | + * public class Node { |
| 32 | + * public var val: Int |
| 33 | + * public var prev: Node? |
| 34 | + * public var next: Node? |
| 35 | + * public var child: Node? |
| 36 | + * public init(_ val: Int) { |
| 37 | + * self.val = val |
| 38 | + * self.prev = nil |
| 39 | + * self.next = nil |
| 40 | + * self.child = nil |
| 41 | + * } |
| 42 | + * } |
| 43 | + */ |
| 44 | +
|
| 45 | +class Solution { |
| 46 | + func flatten(_ head: Node?) -> Node? { |
| 47 | + if head == nil {return head} |
| 48 | + _ = dfs(head) |
| 49 | + return head |
| 50 | + } |
| 51 | + func dfs(_ head: Node?) -> Node? { |
| 52 | + if head == nil {return nil} |
| 53 | + var current = head |
| 54 | + while true { |
| 55 | + if current?.child != nil { |
| 56 | + let childTail = dfs(current?.child) |
| 57 | + childTail?.next = current?.next |
| 58 | + current?.next?.prev = childTail |
| 59 | + current?.next = current?.child |
| 60 | + current?.next?.prev = current |
| 61 | + current?.child = nil |
| 62 | + } |
| 63 | + if current?.next == nil { |
| 64 | + break |
| 65 | + } |
| 66 | + current = current?.next |
| 67 | + } |
| 68 | + return current |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +```typescript= |
| 75 | +// Iterative in TS |
| 76 | +
|
| 77 | +function flatten(head: Node | null): Node | null { |
| 78 | + let curr = head; |
| 79 | + let nextStack: Node[] = []; |
| 80 | + while(curr instanceof Node) { |
| 81 | + if (curr.child instanceof Node) { |
| 82 | + if (curr.next instanceof Node) { |
| 83 | + nextStack.push(curr.next); |
| 84 | + } |
| 85 | + linkNodes(curr, curr.child); |
| 86 | + curr.child = null; |
| 87 | + curr = curr.next; |
| 88 | + } |
| 89 | + else { |
| 90 | + if (curr.next == null && nextStack.length > 0) { |
| 91 | + linkNodes(curr, nextStack.pop()); |
| 92 | + } |
| 93 | + curr = curr.next; |
| 94 | + } |
| 95 | + } |
| 96 | + return head; |
| 97 | +}; |
| 98 | +
|
| 99 | +function linkNodes(prev: Node, next: Node): void { |
| 100 | + prev.next = next; |
| 101 | + next.prev = prev; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
0 commit comments