关于LinkedList的一些信息

时间:2019-01-27 07:13:01

标签: java list linked-list

我是Java的新手,在学习LinkedList时遇到了一些麻烦。

我陷入了这个问题: 编写一个takeSmallerFrom方法,该方法比较两个整数列表,确保第一个列表在相应位置具有较小的值。列表中的任何一个都可以长于另一个。

我的尝试解决方案如下:

public void takeSmallerFrom(LinkedIntList other) {
if(front != null && other.front != null) {
    ListNode temp = front.next;
    ListNode prev = front;
    if(front.next != null && other.front != null) {
        if(front.data > other.front.data) {
            front.next = other.front.next;
            other.front.next = temp;
            front = other.front;
            other.front = prev;
        }
        ListNode current = other.front;
        while(prev.next.next != null && current.next.next != null) {
            if(prev.next.data > current.next.data) {
                temp = prev.next;
                prev.next = current.next;
                current.next = temp;
                temp = temp.next;
                current.next.next =prev.next.next;
                prev.next.next = temp; 
            } 
            prev = prev.next;
            current = current.next;
        }
    }
}

结果表明,我的解决方案在第一个ListNode之后失败了,但是我找不到其余的失败的原因。我真的很固执,如果有人可以帮助我,谢谢!

对不起,我没有弄清楚:“前”是指领导我的LinkedList的头

整个解决方案是针对仅存储int的特殊LinkedList,因此我需要分别比较这些LinkedLists中的每个int值。

0 个答案:

没有答案
相关问题