Java 如何在当前链表中复制我的节点?

Java 如何在当前链表中复制我的节点?,java,while-loop,linked-list,nodes,Java,While Loop,Linked List,Nodes,我很难想出家庭作业的解决办法。我的教授给了我一堆测试链表,希望我复制链表中的每个节点 例如: int[] array = {1, 2, 3, 4, 10}; LinkedIntList list = new LinkedIntList(array); list.stutter(); 打印新列表时的结果应该是:{1,1,2,2,3,3,4,4,10,10} 这是我到目前为止所拥有的。。。(我想不出一个合理的while循环) 我也有很多构造函数,但由于我在方法中调用了一个,

我很难想出家庭作业的解决办法。我的教授给了我一堆测试链表,希望我复制链表中的每个节点

例如:

    int[] array = {1, 2, 3, 4, 10};
    LinkedIntList list = new LinkedIntList(array);
    list.stutter();
打印新列表时的结果应该是:{1,1,2,2,3,3,4,4,10,10}

这是我到目前为止所拥有的。。。(我想不出一个合理的while循环)

我也有很多构造函数,但由于我在方法中调用了一个,下面是相关的构造函数:

public ListNode(int data, ListNode next){
    this.data = data;
    this.next = next;
}

感谢您的帮助

您只需确保指向副本的
下一个
,而不是当前的
。下一个

public void stutter() {
    for (ListNode current = front; current != null; ) {
        ListNode duplicate = new ListNode(current.data, current.next);
        current.next = duplicate;
        current = duplicate.next
    }
}
试试这个

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;
      }

您当前的代码将无法
stutter
仅包含一个元素的列表因此我需要帮助@smack89谢谢!你是一个生命saver@theNoobJavaCoder只要你明白我为什么在那里做这些事,没问题。德国劳埃德船级社
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;
      }