Merge pull request #1337 from tan-i-ham/master

chore: Add new solution to LeetCode 203
This commit is contained in:
程序员Carl
2022-06-06 11:36:14 +08:00
committed by GitHub

View File

@ -266,6 +266,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