class ListNode{
int data;
ListNode next;
}
public class Link {
public static void InsertPosFront(ListNode pos,int data){
ListNode node = new ListNode();
node.data = pos.data; // 创建新节点(数据就为pos位置的数据)
node.next = pos.next;
pos.next = node; //插在pos位置的后面
pos.data = data; //然后将要插入的数据放在pos位置
}
}
public static void print(ListNode head){
while(head != null){
System.out.println(head.data);
head = head.next;
}
}
public static void main(String[] args) {
ListNode n1 = new ListNode();
ListNode n2 = new ListNode();
ListNode n3 = new ListNode();
ListNode n4 = new ListNode();
n1.data = 1;
n2.data = 2;
n3.data = 3;
n4.data = 4;
n1.next = n2;
n2.next = n3;
n3.next = n4;
InsertPosFront(n2,2);
print(n1);
}
}