Skip to content

LeetCode 讀書會第 19 次聚會 2021/01/12 #8

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 3 commits into from
Jan 13, 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
205 changes: 205 additions & 0 deletions 紀錄/LeetCode 讀書會第 19 次聚會 2021_01_12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# LeetCode 讀書會第 19 次聚會 2021/01/12

leetcode 讀書會通知
1. 項目: 第 19 次聚會
2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法
3. 時間: 01/12 (二) 20:00 ~ 21:00
4. 地點: google meet 線上 (前 10 分鐘預備鏈接)
5. 解題項目: [Linked list - classic problem 的最後一題 Remove Nth Node From End Of List](https://leetcode.com/explore/learn/card/linked-list/219/classic-problems/)

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

通知 9 人: @游諭, @turtle, @james, @Louis, @TzuHsuan

備註: 這次的題目是:


1. 203 - easy: Remove Linked List Elements
2. 328 - Medium: Odd Even Linked List
3. 234 - easy:Palindrome Linked List


請在 issue 回報一下,我會把你加入 contribution

https://zh.wikipedia.org/wiki/差一错误

# Remove Linked List Elements

```swift
// swift
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
let dummy = ListNode(0, head)
var prev: ListNode? = dummy
var current: ListNode? = head

while(current != nil) {
if current?.val == val { // 檢查 val
prev?.next = current?.next
} else {
prev = current // 更新 prev
}
current = current?.next // 往後走
}
return dummy.next
}
```

```swift
// swift
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
let dummy = ListNode(val &+ 1, head)
var current: ListNode? = dummy

while(current != nil) {

if current?.next?.val == val { // 檢查 val
current?.next = current?.next?.next
} else {
current = current?.next // 往後走
}
}
return dummy.next
}
}
```

# Odd Even Linked List


# Palindrome Linked List

## Recursive

```swift=
// swift
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
/*

1
11
121 2 11
1221 22 11
1231 23 11
*/
func isPalindrome(_ head: ListNode?) -> Bool {
var ptr: ListNode? = head
func helper(_ head: ListNode?) -> Bool {
if head == nil {return true}
if helper(head?.next) == false {return false}
if (head?.val != ptr?.val) {return false}
ptr = ptr?.next
return true
}

return helper(head)
}
}
```

## Iterative & in-place

遍歷整個 List 3 次
1. 計算長度
2. 反轉後半段
3. Two pointer 從兩端檢查是否相同

```typescript=
// TypeScript

/**
Iterative & in-place
time: O(3n) = O(n), space: O(1)
*/
function isPalindrome(head: ListNode | null): boolean {
// 算出長度
const length = getListLength(head);
if (length <= 1) return true;
// 反轉後半段,並額外考慮長度為偶數的情況
const middleNode = getNthNode(head, Math.ceil(length / 2));
const anotherHead = length % 2 === 0 ? reverseList(middleNode.next) : reverseList(middleNode);
middleNode.next = null;
// Two pointer 從兩端檢查是否相同
return isSameList(head, anotherHead);
};

// 以下為輔助函式
function isSameList(headA: ListNode, headB: ListNode): boolean {
let currA = headA;
let currB = headB;
while(currA != null) {
if (currA.val !== currB.val) return false;
currA = currA.next;
currB = currB.next;
}
if (currB == null) return true;
else return false;
}

function getListLength(head: ListNode): number {
let count = 0;
let curr = head;
while(curr != null) {
count++;
curr = curr.next;
}
return count;
}

function getNthNode(head: ListNode, n: number): ListNode {
if (head == null || !Number.isInteger(n) || n < 1) return null;
let count = 1;
let curr = head;
while(count < n) {
curr = curr?.next;
count++;
}
return curr;
}

/** Iteratively */
function reverseList(head: ListNode | null): ListNode | null {
if (!head) return null;
let curr = head;
let prev: ListNode = null;
let next: ListNode = null;
while(curr) {
next = curr.next ?? null;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
};
```



![](https://i.imgur.com/UJSIGtG.png)

比較了 Recursive vs Iterative

Runtime: 92 vs 100 ms

Memory: 45.7 vs 42.3 MB