Data Structure I 筆記撰寫計畫
 敘述
這是 Data Structure I 的第八天第三個題目,總共有三題。
傳入一個 Linked List 的 head ,然後再傳入一個數字,移除這個 Linked List 裡所有等於這個數字的節點,然後回傳 head 。
點我開啟限制與範例
限制:
- The number of nodes in the list is in the range [0, 10^4].
- 1 <= Node.val <= 50
- 0 <= val <= 50
Example 1:
![https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg]()
| 12
 
 | Input: head = [1,2,6,3,4,5,6], val = 6Output: [1,2,3,4,5]
 
 | 
Example 2:
| 12
 
 | Input: head = [], val = 1Output: []
 
 | 
Example 3:
| 12
 
 | Input: head = [7,7,7,7], val = 7Output: []
 
 | 
 筆記
判斷 head.next.val 是否是要被移除的,如果是,那就把 head.next 連結到 head.next.next ,這樣就等於是 remove this node 了。
 程式碼
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 
 | 
 
 
 
 var removeElements = function (head, val, outHead) {
 
 if (head && !outHead && head.val == val) {
 while (head.val == val) {
 head = head.next;
 if (head == null) return head;
 }
 outHead = head;
 } else if (!outHead) {
 outHead = head;
 }
 
 if (!head || !head.next) return outHead ? outHead : head;
 
 if (head.val == val) {
 head = head.next;
 } else if (head.next.val == val) {
 while (head.next && head.next.val == val) {
 head.next = head.next.next;
 }
 }
 
 return removeElements(head.next, val, outHead);
 };
 
 | 
 成績

點我開啟舊寫法/失敗寫法