【问题标题】:Linked List Concatenation链表连接
【发布时间】:2013-03-04 21:07:10
【问题描述】:

为分配创建链表,其中一个要求是一个名为 concat 的方法,它接受一个列表参数并将其附加到当前列表的末尾。这种方法没有必要使用递归,但我们的程序应该大量使用递归。我只是想知道是否有可能为此提出一个递归算法。我们的列表类只有一个头节点,没有别的,而且它们没有双向链接。

我当前的尝试只能递归地附加第一个值。我知道它做错了什么,但我无法提出解决方案。第一种方法是在列表中实际调用的方法,其中将列表传递给“连接”。然后我尝试找到列表的尾部并将它们传递给递归方法。这种“包装”方法是我们递归方法的强制性要求。这是我的尝试,但显然失败了,因为一旦所有调用从堆栈中弹出然后重新进入对 concat。如果递归可能,您能否告诉我如何将此值推进到第一个列表并重新输入递归调用,或者可能只是解决此问题的更好的通用方法?感谢您的宝贵时间。

public void concat(MyString list1) {
    CharacterNode tail = null, pt = list1.head;
    // Find the tail of the list
    if (pt == null) {
    } else if (pt.getNext() == null) {
        tail = pt;
    } else {
        while (pt.getNext() != null) {
            pt = pt.getNext();
        }
        tail = pt;
    }
    list1.head = concat(list1.head, tail, list1.head);
}
private static CharacterNode concat(CharacterNode lhead, CharacterNode tail, CharacterNode pt) {
    // Pass in smaller list every time
    // Head traverses down list till the end
    // Add new node with (pt's letter, null link)
    if (lhead == null) {
    // If head is null, we need to add the node
        lhead = new CharacterNode(pt.getCharacter(),null);
    } else if (tail.getNext() == lhead) {
    // If the head is past tail, stop   
    } else {
        // Call concat on a smaller list
        lhead.setNext(concat(lhead.getNext(),tail,pt));
    }
    return lhead;
}

这里是 CharacterNode:

class CharacterNode {
    private char letter;
    private CharacterNode next;

    public CharacterNode(char ch, CharacterNode link) {
        letter = ch;
        next = link;
    }

    public void setCharacter(char ch) {
        this.letter = ch;
    }

    public char getCharacter() {
        return letter;
    }

    public void setNext(CharacterNode next) {
        this.next = next;
    }

    public CharacterNode getNext() {
        return next;
    }
}

我的字符串:

class MyString {
    // member variable pointing to the head of the linked list
    private CharacterNode head;

    // default constructor
    public MyString() {
    }

    // copy constructor
    public MyString(MyString l) {
    }

    // constructor from a String
    public MyString(String s) {
    }

    // for output purposes -- override Object version
    // no spaces between the characters, no line feeds/returns
    public String toString() {
    }

    // create a new node and add it to the head of the list
    public void addHead(char ch) {
    }

    // create a new node and add it to the tail of the list -- "wrapper"
    public void addTail(char ch) {
    }

    // Recursive method for addTail
    private static CharacterNode addTail(CharacterNode L, char letter) {
    }

    // modify the list so it is reversed
    public void reverse() {
    }

    // remove all occurrences of the character from the list -- "wrapper"
    public void removeChar(char ch) {
    }

    // Recursive removeChar method
    private static CharacterNode removeChar(CharacterNode n, char letter) {
    }

    // "wrapper" for recursive length()
    public int length() {
    }

    // Returns the length of the linked list
    private static int length(CharacterNode L) {
    }

    // concatenate a copy of list1 to the end of the list
    public void concat(MyString list1) {
    }

    // recursive method for concat
    private static CharacterNode concat(CharacterNode lhead, CharacterNode tail, CharacterNode pt) {
    }
}

【问题讨论】:

  • 除非我们看到 MyString 和 CharacterNode 的定义,否则我们无法回答这个问题。
  • 作为旁注,因为递归是您的程序的一个关键概念(并且您询问是否可以在您的情况下使用递归),任何您可以迭代地(使用循环)做的事情,您也可以递归地做,反之亦然。
  • @Ingo 为你添加了他们两个!
  • @ajp15243 谢谢,我不知道你可以做任何迭代解决方案可以用递归做的事情。很高兴知道!

标签: java recursion linked-list


【解决方案1】:

要连接两个链表,你必须使第一个链表的最后一个节点指向第二个链表的第一个节点。

Node first_list = ...  // head node
Node second_list = ... // head node
...
Node last_node = first_list.getLastNode()
last_node.setNext(second_list)

现在专注于实现getLastNode()。它可以通过使用递归或迭代来非常简单地完成,字面意思是 2 行。

【讨论】:

  • 感谢您这么快回复!这似乎可以解决问题! :)
猜你喜欢
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多