原题:根据有序表L2的值删除链表L1的节点。比如:L1 = {A,B,C,D,E,F}, L2={1,2,3,10}
那么删除之后L1={A,E,F},因为节点10不存在
下面是节点类(也为了其他测试的用途,实现了一个copy接口)
下面是主函数方法~
其中L2我采用了一个数组来存储
那么删除之后L1={A,E,F},因为节点10不存在
下面是节点类(也为了其他测试的用途,实现了一个copy接口)
public class Node implements Cloneable {
public String value;
public Node next;
public Node(String value, Node next) {
this.value = value;
this.next = next;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
下面是主函数方法~
其中L2我采用了一个数组来存储
public class MinusList {
public static void main(String[] args) {
Node a0 = new Node("a0",null);
Node a1 = new Node("a1",null);
Node a2 = new Node("a2",null);
Node a3 = new Node("a3",null);
Node a4 = new Node("a4",null);
Node a5 = new Node("a5",null);
Node a6 = new Node("a6",null);
a0.next = a1;
a1.next = a2;
a2.next = a3;
a3.next = a4;
a4.next = a5;
a5.next = a6;
int[] array = {2,4,8};
Node head = a0;
Node t = head;
Node t2;
for(int i=0; i < array.length; i++) {
int steps = array[i];
if(i != 0) {
steps = array[i] - array[i-1];
}
for(; (steps > 1) && (t != null); steps--) { //让t指向目标节点的上一个节点
t = t.next;
}
if(t != null) {
t2 = t.next;
t.next = t.next.next;
t2 = null;
}
}
//print all nodes
t = head;
while(t != null) {
System.out.print(t.value + " ");
t = t.next;
}
}
}