我正在完成一个与链表相关的小项目。我还有一个方法要完成,然后就完成了。我已经完成了以下所有方法。
- 添加到前面
- 添加到后面
- 删除前面的节点
- 删除后面的节点
- 删除指定位置的节点
- 删除指定节点之前的节点
- 删除指定节点之后的节点
- 将当前节点移动到链表末尾
moveRear
方法。public void moveRear(int index) {
if (index < 1 || index > size || index == 1 || index == size) {
System.out.println("Conditions are not met");
return;
}
if (head == null) {
System.out.println("List is empty");
return;
}
Node cNode = null;
Node pNode = null;
cNode = head;
for (int i = 1; i < index - 1; i++) {
cNode = cNode.next;
}
pNode = head;
while (pNode.next != null) {
pNode = pNode.next;
}
cNode = cNode.next.next;
pNode = cNode.next;
tail = cNode.next;
}
我需要将前一个节点链接到我要移动的节点后面。例如,我想把节点3移到链表的尾部。这意味着我需要让节点2链接到节点4以保持链表连接。然后我需要让最后一个节点指向节点3,最后使TAIL指向它。