删除链表中等于给定值 key 的所有节点

public void removeAllKey(int Key){
    Node prev = this.head;
    Node cur = this.head.next;
    while(cur != null) {
        if(cur.data == key) {
            prev.next = cur.next;
            cur = cur.next;
        }else {
            prev = prev.next;
            cur = cur.next;
        }
    }
    if(this.head.data == key) {
        this.head = this.head.next;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45755718/article/details/105212543