Leetcode 203. Remove Linked List Elements
方法1: iterate。這題主要是要設定一個prev node,然後還要設定一個哨兵node,就這個比較煩,因為遍歷的時候會修改原來的list導致最後不知道返回什麼了(我經常做到最後不知道要返回什麼)。時間複雜n,空間複雜1.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode curr = head;
ListNode sentinel = new ListNode(0);
sentinel.next = head;
ListNode prev = sentinel;
while(curr != null){
if(curr.val == val){
prev.next = curr.next;
}else{
prev = prev.next;
}
curr = curr.next;
}
return sentinel.next;
}
}
方法2: recursion。直接上程式碼,非常簡單易懂。時間n,空間1.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}
}
總結:
- 感覺遇到連結串列題我一般都會先想到iterate,卻沒有花多少精力在recursion上,其實recursion也是做連結串列題的很好的方法。
相關文章
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- LeetCode | 141 linked list cycleLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- Leetcode 206. Reverse Linked ListLeetCode
- Leetcode 234. Palindrome Linked ListLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- 資料結構與演算法 | Leetcode 141:Linked List Cycle資料結構演算法LeetCode
- 328. Odd Even Linked List
- 92. Reverse Linked List II
- 資料結構與演算法 | Leetcode 876. middle-of-the-linked-list資料結構演算法LeetCode
- Remove-duplicates-from-sorted-listREM
- Java List的remove()方法陷阱JavaREM
- [leetcode]remove-elementLeetCodeREM
- [LeetCode Python 3] 876. Middle of the Linked List(連結串列的中間結點)LeetCodePython
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- 114-Flatten Binary Tree to Linked List
- leetcode-27. Remove ElementLeetCodeREM
- Leetcode 27 Remove-ElementLeetCodeREM
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- LeetCode 之 JavaScript 解答第141題 —— 環形連結串列 I(Linked List Cycle I)LeetCodeJavaScript
- 資料結構與演算法 | Leetcode 19. Remove Nth Node From End of List資料結構演算法LeetCodeREM
- 【程式碼優化】List.remove() 剖析優化REM
- [LeetCode] 402. Remove K DigitsLeetCodeREMGit
- Leetcode 160. Intersection of Two Linked ListsLeetCode