public ListNode RemoveElements(ListNode head, int val) { if (head == null) { return head; }
// 設定虛擬節點,避免刪除 head 的情況要特別處理 ListNode dummy = new ListNode(-1, head); // 前一個節點 ListNode pre = dummy; // 當前節點 ListNode cur = head;
while (cur != null) { // 發現要刪除的節點 if (cur.val == val) { // 將前一個節點的 next 指向當前節點的 next pre.next = cur.next; } // 不需刪除則將 pre 和 cur 都往後移動,以便檢查下一個節點 else { pre = cur; } cur = cur.next; } return dummy.next; }
因為習慣在 ide 上寫完再貼到 leetcode 提交,這邊附上寫得很醜的 GenerateListNodes 方法XD
Design your implementation of the linked list. You can choose to use a singly or doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Implement the MyLinkedList class: - MyLinkedList() Initializes the MyLinkedList object. - int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1. - void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. - void addAtTail(int val) Append a node of value val as the last element of the linked list. - void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted. - void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.
Explanation MyLinkedList myLinkedList = new MyLinkedList(); myLinkedList.addAtHead(1); myLinkedList.addAtTail(3); myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3 myLinkedList.get(1); // return 2 myLinkedList.deleteAtIndex(1); // now the linked list is 1->3 myLinkedList.get(1); // return 3
Constraints:
1 2 3
0 <= index, val <= 1000 Please do not use the built-in LinkedList library. At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.
// 在第 index 個節點前面新增一個節點 // 如果 index <= 0,那麼新增的節點就是新的 head // 如果 index == List 長度,那麼新增的節點就是新的 tail // 如果 index > List 長度,則直接 return publicvoidAddAtIndex(int index, int val) { if (index > size) { return; } if (index < 0) { index = 0; }
ListNode pre = head; // 執行 index 次(因為包含虛擬 head),找到欲新增 index 的前一個節點 for (int i = 0; i < index; i++) { pre = pre.next; }
// Add // 新節點 next 指向 pre.next ListNode needAdd = new ListNode(val, pre.next); // pre 指向新節點 pre.next = needAdd;
// 更新 list 長度 size++; }
// 刪除第 index 個節點 publicvoidDeleteAtIndex(int index) { if (index < 0 || index >= size) { return; }
ListNode pre = head; // 執行 index 次(因為包含虛擬 head),找到欲刪除 index 的前一個節點 for (int i = 0; i < index; i++) { pre = pre.next; } pre.next = pre.next.next;
// 更新 list 長度 size--; }
publicvoidPrintList() { ListNode curr = head; for (int i = 0; i < size; i++) { curr = curr.next; Console.WriteLine(curr.val); } } }
用法範例:
1 2 3 4 5 6 7 8
var t = new MyLinkedList(); t.PrintList(); // nothing t.AddAtHead(1); t.AddAtTail(3); t.AddAtIndex(1, 2); t.PrintList(); // 1, 2, 3 t.DeleteAtIndex(0); t.PrintList(); // 2, 3