如何轮流组合2个双链表

时间:2016-11-20 09:20:40

标签: java algorithm linked-list

我在这里有2个双链表,我想要合并到新列表中,但我有问题要轮流制作。到目前为止,这是我的代码:

class NodeDLL {

public Object data;
public NodeDLL next;
public NodeDLL prev;

public NodeDLL() {
    next = null;
}}

public class DoubleLinkList {

private NodeDLL head, tail;
private int counter, pos;

public DoubleLinkList() {
    head = null;
    tail = null;
    counter = -1;
    pos = 0;
}


public void InsertFirst(Object dt) {
    NodeDLL newNode = new NodeDLL();
    newNode.data = dt;
    if (head == null) {
        newNode.prev = head;
        newNode.next = tail;
        head = newNode;
        tail = newNode;
        counter = 0;
    } else {
        newNode.next = head;
        head.prev = newNode;
        head = newNode;

    }
    counter++;
}

public static DoubleLinkList combine(DoubleLinkList L1, DoubleLinkList L2) {

    DoubleLinkList L3 = new DoubleLinkList();
    DoubleLinkList L4 = new DoubleLinkList();

    while (L1.head != null) {
        L3.InsertFirst(L1.head.data);
        L1.head = L1.head.next;
    }
    while (L2.head != null) {
        L3.InsertFirst(L2.head.data);
        L2.head = L2.head.next;
    }
    while (L3.head != null) {
        L4.InsertFirst(L3.head.data);
        L3.head = L3.head.next;
    }
    return L4;

}

 public void Print(String kom) {
    System.out.println(kom);
    NodeDLL p = head;
    while (p != tail.next) {
        System.out.print(p.data + " <-> ");
        p = p.next;
    }
    System.out.println();
}

public static void main(String s[]) {

    DoubleLinkList dll2 = new DoubleLinkList();
    dll2.InsertFirst("A");
    dll2.InsertFirst("B");
    dll2.InsertFirst("C");
    dll2.InsertFirst("D");
    dll2.InsertFirst("E");
    dll2.InsertFirst("F");
    dll2.Print("Linked List 1");
    System.out.println("");

    DoubleLinkList dll3 = new DoubleLinkList();
    dll3.InsertFirst(1);
    dll3.InsertFirst(2);
    dll3.InsertFirst(3);
    dll3.InsertFirst(4);
    dll3.InsertFirst(5);
    dll3.InsertFirst(6);
    dll3.Print("Linked List 2");
    System.out.println("");

    DoubleLinkList NewList= DoubleLinkList.combine(dll2, dll3);
    NewList.Print("Result");
}}

此代码将显示:

  

链接列表1

     

F&lt; - &gt; E - &gt; D - &lt; - &gt; C - C - 。 B - &lt; - &gt; A - &lt; - &gt;

     

Linked List 2

     

6 - &lt; - &gt; 5 - &lt; - &gt; 4 - &lt; - &gt; 3 - &lt; - &gt; 2 - &lt; - &gt; 1 - &lt; - &gt;

     

结果

     

F&lt; - &gt; E - &gt; D - &lt; - &gt; C - C - 。 B - &lt; - &gt; A - &lt; - &gt; 6 - &lt; - &gt; 5 - &lt; - &gt; 4 - &lt; - &gt; 3 - &lt; - &gt; 2 - &lt; - &gt; 1&lt; - &gt;

预期产出:

  

F&lt; - &gt; 6 - &lt; - &gt; E - &gt; 5 - &lt; - &gt; D - &lt; - &gt; 4 - &lt; - &gt; C - C - 。 3 - &lt; - &gt; B - &lt; - &gt; 2 - &lt; - &gt; A - &lt; - &gt; 1

2 个答案:

答案 0 :(得分:1)

/**
 * This method takes two DoublyLinkedList's and combines them one into one
 * list with alternating items from each input lists.
 *
 * @param     L1   the first linked list
 * @param     L2   the second linked list
 * @return    L3   the merged linked list
 */
public static DoubleLinkList combine(DoubleLinkList L1, DoubleLinkList L2) {
    DoubleLinkList L3 = new DoubleLinkList();
    DoubleLinkList L4 = new DoubleLinkList();

    // insert both list until the shorter list reaches its end
    while (L1.head != null && L2.head != null) {
        L4.InsertFirst(L1.head.data);
        L4.InsertFirst(L2.head.data);
        L1.head = L1.head.next;
        L2.head = L2.head.next;
    }

    // now check which list is longer
    if (L1.head == null && L2.head != null)
        L1.head = L2.head;

    // now just insert the rest of the items from the longer list
    while (L1.head != null) {
        L4.InsertFirst(L1.head.data);
        L1.head = L1.head.next;
    }

    // now reverse it by inserting this list into another list
    while (L4.head != null) {
        L3.InsertFirst(L4.head.data);
        L4.head = L4.head.next;
    }

    return L3;
}

为什么不实现只插入列表末尾的方法?这样您就不需要执行最后一个while循环来反转列表。

答案 1 :(得分:0)

由于我没有DoubleLinkedList课程,我无法尝试,所以可能至少有一两个错字:

while (L1.head != null && L2.head != null) {
    // insert one element from each list
    L3.InsertFirst(L1.head.data);
    L1.head = L1.head.next;
    L3.InsertFirst(L2.head.data);
    L2.head = L2.head.next;
}

如果列表的长度不同,您可能希望在已有的循环之前插入此循环;保持它们以确保包含较长列表中的任何元素。