Java 在新列表中复制链接列表中的项目
我正试图复制一个节点列表。 例如,我的列表如下:Java 在新列表中复制链接列表中的项目,java,algorithm,data-structures,Java,Algorithm,Data Structures,我正试图复制一个节点列表。 例如,我的列表如下: Node n = new Node(1,new Node(12, new Node(34, new Node(3, Node.NIL)))); 我的职能是: public Node copy() { Node initial= this; Node duplicate=new Node(initial.getItem()); while(Node.NIL!=initial.getNext()
Node n = new Node(1,new Node(12, new Node(34, new Node(3, Node.NIL))));
我的职能是:
public Node copy() {
Node initial= this;
Node duplicate=new Node(initial.getItem());
while(Node.NIL!=initial.getNext()){
initial=initial.getNext();
Object a = initial.getItem();
duplicate.next=new Node(a);
}
return duplicate;
}
所以当我这样做时,输出列表是重复的[1,3]。我不明白12和34在哪里。在这一步上
复制。下一步=新节点(a)
您每次都会覆盖以前的复制值。下一次
。创建下一个节点时,应在每个步骤上更改duplicate
上的引用
可以使用递归创建下一个节点的副本,然后创建新节点:
public Node copy() {
Node initial= this;
Node copyNext = this.getNext() == NIL? NIL : this.getNext().copy();
Node duplicate = new Node(initial.getItem());
duplicate.next = copyNext;
return duplicate;
}
没有递归:
public Node copy() {
Node currentNode= this;
Node firstDuplicate = new Node(currentNode.getItem()); //save reference for first node to return
Node currentDuplicate=firstDuplicate;
while(Node.NIL!=currentNode.getNext()){
Node nextNode = currentNode.getNext();
Node nextCopy = new Node(nextNode.getItem()); //create copy of next
currentDuplicate.next = nextCopy; //assign this copy as next for current duplicated node
currentDuplicate = nextCopy; //change reference for current duplicated node to copy
currentNode = nextNode;
}
return firstDuplicate;
}
如果我理解正确,您需要创建还原列表。在这种情况下,您不需要创建初始列表的新副本
public Node reverse() {
Node head = NIL; //initial we have a empty list
Node current = this; //set cursor to current node
while (current != NIL) {
Node copy = new Node(current.getItem()); //create a copy of current node
copy.next = head; //set head node as next for copy
head = copy; //now move head to copy
current = current.next; // move cursor for next position
}
return head;
}
要使用递归创建反向列表,您只需使用其他方法在先前创建的副本上保留引用:
public Node reverse() {
if (this == NIL) {
return NIL;
}
return reverse(new Node(this.getItem(), NIL), this.getNext());
}
private Node reverse(Node head, Node tail) {
Node copy = new Node(tail.getItem());
copy.next = head;
if (tail.getNext() == NIL) {
return copy;
}
return reverse(copy, tail.next);
}
您没有正确更新重复列表,它将只包含原始列表的第一个和最后一个节点;在循环结束之前。您需要在调试器中逐步完成代码,但问题是您只创建了一个节点,该节点只有下一个值,即长度必须为2。确定。我该怎么办?。我测试了很多东西,但我还是对链表感兴趣,我建议带个便笺簿和笔可能会有帮助。用节点和节点之间的指针绘制初始列表。然后手工按照你的代码,更新你绘图中的指针。我考虑过递归。我不能用递归。我简化了问题。这最初不是一个函数;我想把它放在一个反向函数中。所以,如果我的函数中有反向函数,我就不能递归调用它。@MohamedMoka更新了我的答案,没有递归方法。我问这个问题是因为我试图做一个反向函数并保留初始列表。但当我运行它时,我的反向列表是唯一的列表。这就是为什么我想复制一个列表,然后使用这个列表并将其反转。所以我在我的函数中加了你一点,但它仍然不起作用。我用你的一点向你展示我的全部功能:@MohamedMoka这是你想要做的吗?你说得对,唯一的问题是我想做recursively@VladBochenin以上是我的全部问题
public Node reverse(){
Node p= this;
Node firstDuplicate = new Node(p.getItem()); //save reference for first node to return
Node currentDuplicate=firstDuplicate;
while(Node.NIL!=p.getNext()){
Node nextNode = p.getNext();
Node nextCopy = new Node(nextNode.getItem());
currentDuplicate.n = nextCopy;
currentDuplicate = nextCopy;
p = nextNode;
}
/* If the list is empty */
if(firstDuplicate == NIL)
return Node.NIL;
/* If the list has only one node */
if(firstDuplicate.n == Node.NIL)
return firstDuplicate;
// Node reverseRest = new Node(p.getItem(),Node.NIL);
Node rest = new Node();
rest = firstDuplicate.getNext();
firstDuplicate.setNext(Node.NIL);
// Node reverseRest=new Node(p.getItem(),reverseRest);
Node reverseRest=new Node();
reverseRest = rest.reverse();
/* Join the two lists */
rest.setNext(firstDuplicate);
//p=this;
// p=p.nthNext(0);
return reverseRest;
}