203-移除链表元素

删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

 public ListNode removeElements(ListNode head, int val) {
        ListNode a=head;
        while(a!=null){
            if(a.val==val){
                ListNode x=a.next;
                a.next=null;
                a=x;
                head=x;
            }else if(a.next!=null&&a.next.val==val){
                ListNode x=a.next;
                a.next=x.next;
            }else{
                a=a.next;
            }
        }
        return head;
    }

猜你喜欢

转载自www.cnblogs.com/dloading/p/10864105.html