/**
* 在单向链表中删除指定的key
*/
public class RemoveSpecKeyFromNode {
public static class Node<T> {
public T value;
public Node next;
public Node(T node) {
this.value = node;
}
}
public static <T> Node removeSpecKeyFromNode(Node node, T key) {
if (node == null || key == null) {
return node;
}
while (node.value == key) {
node = node.next;
}
Node pre = node;
Node cur = null;
while ((cur = node.next) != null) {
if (cur.value == key) {
pre.next = cur.next;
} else {
pre = cur;
}
}
return node;
}
}
/* 如有错误,欢迎批评指正 */