在节点列表中复制所有内容

时间:2014-08-03 17:34:56

标签: java recursion linked-list nodes

此方法,doubleList应该采用列表并再次重复节点序列。 LinkedIntList应该表现得像整数的ListNode。例如,[1] - > [2] - > [3]将是[1] - > [2] - > [3] - > [1] - > [2] - > [3]。虽然我设法让它使用辅助方法add(),我应该不使用帮助器。我该怎么做呢?

这是ListNode:

public class ListNode {
    public int data;       // data stored in this node
    public ListNode next;  // link to next node in the list

    // post: constructs a node with data 0 and null link
    public ListNode() {
        this(0, null);
    }

    // post: constructs a node with given data and null link
    public ListNode(int data) {
        this(data, null);
    }

    // post: constructs a node with given data and given link
    public ListNode(int data, ListNode next) {
        this.data = data;
        this.next = next;
    }
}

这是带有帮助程序的LinkedIntList:

public class LinkedIntList {
    private ListNode front;  // first value in the list

    // post: constructs an empty list
    public LinkedIntList() {
        front = null;
    }

    // post: doubles size of array by appending copy of original into end
    public void doubleList() {
        ListNode current = front; // front of this list
        LinkedIntList other = new LinkedIntList(); // stores copy of the original list
        ListNode temp = other.front; // front of other list

        while (current != null) { // puts a copy of current into new list
            other.add(current.data); // adds data to it
            current = current.next;
        }

        temp = other.front; // resets other list to beginning
        while (temp != null) { // readds that copy onto end of this list
            add(temp.data);
            temp = temp.next;       
        }   
    }

    // post: appends the given value to the end of the list (helper)
    public void add(int value) {
        if (front == null) {
            front = new ListNode(value);
        } else {
            ListNode current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new ListNode(value);
        }
    } 
}   

这是我没有使用助手的尝试。这有什么问题?它似乎没有将其他列表附加到此列表的末尾。

   public void doubleList() {
        ListNode current = front; // front of this list
        LinkedIntList other = new LinkedIntList(); // stores copy of the original list
        ListNode temp = other.front; // front of other list

        while (current != null) { // puts copy of current onto temp
            temp = new ListNode(current.data);
            temp = temp.next;
            current = current.next;
        }
        temp = other.front; // reset temp to front
        while (temp != null) { // goes through temp to add to end of current
            current = new ListNode(temp.data);
            temp = temp.next;
        }
    } 

1 个答案:

答案 0 :(得分:0)

第二个doubleList()方法的问题:

这些行:

    LinkedIntList other = new LinkedIntList(); // stores copy of the original list
    ListNode temp = other.front; // front of other list

相当于:

    LinkedIntList other = new LinkedIntList(); // stores copy of the original list
    ListNode temp = null;

由于front在新列表中为空。

此外,

        temp = new ListNode(current.data);
        temp = temp.next;

相当于:

        temp = new ListNode(current.data);
        temp = null;

这意味着您要创建彼此未连接的节点。

要链接节点,必须更新temp.next。

所以你可以用这种方式创建重复的节点:

    current = front; // front of this list
    ListNode prev = null;
    ListNode frontOther = null;
    while (current != null) {
        temp = new ListNode(current.data);
        if (prev != null) {
            prev.next = temp; // link the previous node to the new node
        } else {
            frontOther = temp; // keep a reference to the first node of the new nodes
        }
        prev = temp;
        current = current.next;
    }

然后您可以将它们连接到原始列表的末尾:

    current = front; // front of this list
    while (current.next != null) {
        current = current.next;
    }
    current.next = frontOther; // link the end of the original list to the duplicated nodes