diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 67776529..fe78ddab 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -266,6 +266,27 @@ public ListNode removeElements(ListNode head, int val) { } 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: