- 删除第一次出现关键字为key的节点
/**
* 找到关键key的前驱,如果有返回前驱节点的引用
* 如果没有返回null
* @param key
* @return
*/
public Node searchPrev(int key) {
Node cur = this.head;
while (cur.next != null) {
if(cur.next.val == key) {
return cur;
}
cur = cur.next;
}
return null;//代表没有要删除的key值的前驱
}
public void remove(int key) {
if(this.head == null) {
return;
}
if(this.head.val == key) {
this.head = this.head.next;
return;
}
//正常的进行删除
Node prev = searchPrev(key);
if(prev == null) {
System.out.println("没有这个key的前驱");
}else {
Node del = prev.next;
prev.next = del.next;
}
}
- 删除所有值为key的节点
public void removeAllKey(int key){
Node prev = this.head;
Node cur = prev.next;
while (cur != null) {
if(cur.val == key) {
prev.next = cur.next;
}else {
prev = cur;
}
cur = cur.next;
}
if(this.head.val == key) {
this.head = this.head.next;
}
}
- 已知如下类说明:
public class Test{
private float f=1.0f;
int m=12;
static int n=1;
public static void main(String args[]){
Test t=new Test();
}
}
如下哪些使用是正确的()
题目内容:
A.t.f = 1.0
纠正:此处1.0为double类型,赋值需要改为1.0f
B.this.n
纠正:n为静态变量,不依赖对象,this是当前对象的引用
C.Test.m
纠正:m不是静态变量,无法访问
D.Test.n
正确