Skip to content

21 20210223 linked list #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions 紀錄/21 20210223.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# LeetCode 讀書會第 21 次聚會 2021/02/23

leetcode 讀書會通知
1. 項目: 第 21 次聚會
2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法
3. 時間: 2/23 (二) 20:00 ~ 21:00
4. 地點: google meet 線上 (前 10 分鐘預備鏈接)
5. 解題項目: [Linked list - ](https://leetcode.com/explore/learn/card/linked-list/210/doubly-linked-list/)

```text
3. Medium 430. Flatten a Multilevel Doubly Linked
> List Hint: Try to solve this problem recursively.
4. Medium 708. Insert into a Cyclic Sorted List
> 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.
5. Medium 138. Copy List with Random Pointer
> 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.
```

6. 共筆: GitHub https://github.com/programmingbookclub/Leetcode-club


---


```swift
// Swift Flatten a Multilevel Doubly Linked List


/**
* Definition for a Node.
* public class Node {
* public var val: Int
* public var prev: Node?
* public var next: Node?
* public var child: Node?
* public init(_ val: Int) {
* self.val = val
* self.prev = nil
* self.next = nil
* self.child = nil
* }
* }
*/

class Solution {
func flatten(_ head: Node?) -> Node? {
if head == nil {return head}
_ = dfs(head)
return head
}
func dfs(_ head: Node?) -> Node? {
if head == nil {return nil}
var current = head
while true {
if current?.child != nil {
let childTail = dfs(current?.child)
childTail?.next = current?.next
current?.next?.prev = childTail
current?.next = current?.child
current?.next?.prev = current
current?.child = nil
}
if current?.next == nil {
break
}
current = current?.next
}
return current
}
}
```


```typescript=
// Iterative in TS

function flatten(head: Node | null): Node | null {
let curr = head;
let nextStack: Node[] = [];
while(curr instanceof Node) {
if (curr.child instanceof Node) {
if (curr.next instanceof Node) {
nextStack.push(curr.next);
}
linkNodes(curr, curr.child);
curr.child = null;
curr = curr.next;
}
else {
if (curr.next == null && nextStack.length > 0) {
linkNodes(curr, nextStack.pop());
}
curr = curr.next;
}
}
return head;
};

function linkNodes(prev: Node, next: Node): void {
prev.next = next;
next.prev = prev;
}
```