すべてのノードのリストを削除するJavaのvalの与えられた値に等しいです。

class Solution{
    public ListNode removeElements(ListNode head, int val) {
        ListNode node=new ListNode(-1);
        node.next=head;
        ListNode cur=node;
        while(cur.next!=null){
           if(cur.next.val==val){
               cur.next=cur.next.next;
           }else {
               cur=cur.next;
           }
        }
        return node.next;
    }
}
公開された83元の記事 ウォンの賞賛2 ビュー620

おすすめ

転載: blog.csdn.net/Nabandon/article/details/104069146