两个双链表串联JAVA

两个双链表串联JAVA,java,concatenation,Java,Concatenation,我在实现两个双链接列表的串联时遇到问题。这是我的concat方法。看起来还可以,但结果是我得到了第一个列表的尾部元素和第二个列表的所有元素 public Node<?> concat(Node<?> head1, Node<?> head2) { if(head1 == null) { return head2; } if(head2 == null) { return head1

我在实现两个双链接列表的串联时遇到问题。这是我的concat方法。看起来还可以,但结果是我得到了第一个列表的尾部元素和第二个列表的所有元素

    public Node<?> concat(Node<?> head1, Node<?> head2)
{
    if(head1 == null)
    {
        return head2;
    }
    if(head2 == null)
    {
        return head1;
    }

    Node<?> n = head1;

    while(n.getNext() != null)
    {
        n = n.getNext();
    }
    n.setNext(head2);

    return head1;
}
公共节点concat(节点头1、节点头2)
{
if(head1==null)
{
返回头2;
}
if(head2==null)
{
返回头1;
}
节点n=head1;
while(n.getNext()!=null)
{
n=n.getNext();
}
n、 setNext(头2);
返回头1;
}
编辑:DoubleLinkedList类:

public class DoubleLinkedList<E>
{
protected int size;
protected Node<?> head, tail;

public DoubleLinkedList()
{
    size = 0;
    clear();
}

public void clear()
{
    head = null;
    tail = null;
}

public int size()
{
    return size;
}

public boolean isEmpty()
{
    return head == null;
}

public Node<?> getHead()
{
    return head;
}

public Node<?> getTail()
{
    return tail;
}

public void add(E value)
{
    Node<E> node = new Node<E>(value);
    if(isEmpty())
    {
        head = node;
        tail = node;
    }
    else
    {
        tail.setNext(node);
        node.setPrevious(tail);
        tail = node;
    }
    size++;
}

public int indexOf(E value)
{
    Node<?> find = head;
    for(int i=0;i<size;i++)
    {
        E n = (E}find.getValue();
        if(n.equals(value))
        {
            return i;
        }
        find = find.getNext();
    }

    return -1;
}


public Node<?> concat(Node<?> head1, Node<?> head2)
{
    if(head1 == null)
    {
        return head2;
    }
    if(head2 == null)
    {
        return head1;
    }

    Node<?> n = head1;

    while(n.getNext() != null)
    {
        n = n.getNext();
    }
    n.setNext(head2);
    head2.setPrevious(n);

    return n;
}


private static final class Node<E>
{
    private E value;
    private Node<?> next, previous;

    public Node(E value)
    {
        this(value, null, null);
    }

    public Node(E value, Node<?> n, Node<?> p)
    {
        this.value = value;
        this.next = n;
        this.previous = p;
    }

    public E getValue()
    {
        return value;
    }

    public void setNext(Node<?> n)
    {
        this.next = n;
    }

    public Node<?> getNext()
    {
        return next;
    }

    public void setPrevious(Node<?> p)
    {
        this.previous = p;
    }

    public Node<?> getPrevious()
    {
        return previous;
    }
}
公共类双链接列表
{
保护整数大小;
保护节点头、尾;
公共双链接列表()
{
尺寸=0;
清除();
}
公共空间清除()
{
head=null;
tail=null;
}
公共整数大小()
{
返回大小;
}
公共布尔值为空()
{
返回头==null;
}
公共节点getHead()
{
回流头;
}
公共节点getTail()
{
返回尾;
}
公共无效添加(E值)
{
节点=新节点(值);
if(isEmpty())
{
头部=节点;
尾=节点;
}
其他的
{
tail.setNext(节点);
node.setPrevious(tail);
尾=节点;
}
大小++;
}
公共整数索引(E值)
{
节点查找=头;
对于(int i=0;i n=head1;
while(n.getNext()!=null)
{
n=n.getNext();
}
n、 setNext(头2);
标题2.设置前一个(n);
返回n;
}
私有静态最终类节点
{
私人股本价值;
私有节点下一个,上一个;
公共节点(E值)
{
这个(值,null,null);
}
公共节点(E值、节点n、节点p)
{
这个值=值;
this.next=n;
这是以前的=p;
}
公共E getValue()
{
返回值;
}
公共void setNext(节点n)
{
this.next=n;
}
公共节点getNext()
{
下一步返回;
}
公共void setPrevious(节点p)
{
这是以前的=p;
}
公共节点getPrevious()
{
返回上一个;
}
}

}

不要返回
head1
尝试返回
n

如果看起来没问题,问题是什么?如果没有看到双链接列表的实现,很难看到问题所在,因为它是双链接的,您还应该设置head2@Andremoniy这似乎并不意味着它是好的。在我看来它似乎是好的好吧,但也许我错了,我在请求帮助找出错误的地方:)如果你有一个
getTail()
,不要使用
getNext()
迭代到最后。不幸的是,它没有帮助:/它看起来像这样
,而(n.getNext()!=null){n=n.getNext();}n.setNext(head2);返回n是正确的。仔细检查节点类。
getNext()
setNext()
做得对吗?这也没什么帮助,但我写了不同的方法,它很有效。感谢您的尝试:)