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
val