chore: Add new solution to LeetCode 203

Without using pre Node
This commit is contained in:
h4
2022-05-16 14:51:05 +09:00
committed by GitHub
parent 7c752afaf4
commit 9ac50a9a91

View File

@ -234,6 +234,27 @@ public ListNode removeElements(ListNode head, int val) {
} }
return head; return head;
} }
/**
* 不添加虚拟节点and pre Node方式
* 时间复杂度 O(n)
* 空间复杂度 O(1)
* @param head
* @param val
* @return
*/
public ListNode removeElements(ListNode head, int val) {
while(head!=null && head.val==val){
head = head.next;
}
ListNode curr = head;
while(curr!=null){
while(curr.next!=null && curr.next.val == val){
curr.next = curr.next.next;
}
curr = curr.next;
}
return head;
}
``` ```
Python Python