当前位置:网站首页>[leetcode] day97 remove linked list elements

[leetcode] day97 remove linked list elements

2022-07-07 02:08:00 Upside down, it's a circle

subject

203. Remove linked list elements 【 Simple 】

Answer key

Iterative solution

class Solution {
    
    public ListNode removeElements(ListNode head, int val) {
    
        ListNode newHead=new ListNode(0,head);
        ListNode p=newHead;
        while(p.next!=null){
    
            if(p.next.val==val)
                p.next=p.next.next;
            else
                p=p.next;
        }
        return newHead.next;
    }
}

Time complexity : O ( n ) O(n) O(n)

Spatial complexity : O ( 1 ) O(1) O(1)

原网站

版权声明
本文为[Upside down, it's a circle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061825363086.html