【问题标题】:How to combine 2 double linked list by turns如何轮流组合2个双链表
【发布时间】:2016-11-20 09:20:40
【问题描述】:

我这里有 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 E D C B A

链表 2

6 5 4 3 2 1

结果

F E D C B A 6 5 4 3 2 1

预期输出:

F 6 E 5 D 4 C 3 B 2 A 1

【问题讨论】:

  • 欢迎来到 Stack Overflow!请查看我们的SO Question Checklist 以帮助您提出一个好问题,从而得到一个好的答案。
  • 发布您的DoubleLinkedList 课程,并为上面的输入发布您的预期输出。 A -&gt; 1 -&gt; B -&gt; 2 -&gt; C -&gt; 3 -&gt; D -&gt; 4 -&gt; E -&gt; 5 -&gt; F -&gt; 6. 是您的预期输出吗?
  • 您是否积极处理next,只产生可用的DoubleLinkLists?尝试更详细地描述by turns:它可能会让您知道如何对其进行编码。
  • @RafiduzzamanSonnet 我已经编辑了我的帖子,希望你能理解我的问题
  • @greybeard 当然不,这只是我代码的一部分。我已经发布了完整的

标签: java algorithm linked-list


【解决方案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 循环来反转列表。

【讨论】:

  • 我喜欢方法内 cmets 的级别 - 最重要的是该方法的文档注释。这保持了 nanang 选择呈现的界面 - 无论好坏:它需要反转并防止在“一次操作”中附加整个列表(尾部)。
  • 这真的很聪明。但是我们可以在insert() 方法中保留一个if 条件来检查插入的项目是否为列表。然后我们仍然可以在尾部插入,而不必反转列表。好吧,它解释了规范是什么以及 OP 想要什么:)
【解决方案2】:

由于我没有你的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;
}

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

【讨论】:

  • 我已经发布了我的完整代码,列表可能有不同的长度。我现在得到了我的预期输出
猜你喜欢
  • 2016-09-17
  • 2019-01-12
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多