我是编程新手,我无法弄清楚如何在链表中的特定位置插入节点。它必须插入位置3.非常感谢帮助插入逻辑。
public void ins (Player p)
{
PlayerNode current = head;
PlayerNode previous = head;
PlayerNode pn = new PlayerNode (new Player (p));
int count=0;
if (isEmpty())
{
pn.setNext(head);
head = pn;
++numberOfItems;
}
else
{
if (count != 3)
{
current = current.getNext();
previous.setNext(pn);
pn.setNext(current);
++count;
}
}
}
答案 0 :(得分:2)
为什么不使用builtins?
答案 1 :(得分:0)
Node InsertNth(Node head, int data, int position) {
Node node = head;
if (position == 0){
node = new Node();
node.data = data;
node.next = head;
return node;
}
else {
while(--position > 0){
node = node.next;
}
Node i = node.next;
node.next = new Node();
node.next.data = data;
node.next.next = i;
return head;
}
}