简体   繁体   English

附加两个单链表

[英]Append two singly linked list

I am trying to implement LinkedList using Java, just to test my skills. 我正在尝试使用Java实现LinkedList,只是为了测试我的技能。 I am stuck at one problem, where I have to append two linked lists that I have created. 我遇到了一个问题,我必须附加两个我创建的链表。 I get into an infinite loop here. 我在这里进入一个无限循环。 Is there any way I can improve the code and implement the desired output ? 有什么办法可以改进代码并实现所需的输出吗?

I/Ps : I / P:

List A : 4->3->2->1->0 列表A:4-> 3-> 2-> 1-> 0

List B : 4->3->2->1->0 列表B:4-> 3-> 2-> 1-> 0

O/P should be : 4->3->2->1->0->4->3->2->1->0 O / P应为:4-> 3-> 2-> 1-> 0-> 4-> 3-> 2-> 1-> 0

class List {
    int val;
    List next;

    public List(int val) {
        this.val = val;
    }

    public String toString() {
        String output = "";
        List current = this;

        while (current != null) {
            output += current.val + "->";
            current = current.next;
        }
        return output + "NULL";
    }
}


class AppendLinkedLists {

    static List push(List list, int num) {
        List newList = new List(num);
        newList.next = list;
        return newList;
    }

    static List appendLists(List listA, List listB) {
        if (listA == null)
            return listB;
        else {
            List tempList = listA;
            while (tempList.next.next != null) {
                tempList = tempList.next;
            }
            tempList.next.next = listB;
            return listA;           
        }
    }

    public static void main(String[] args) {
        List listA = new List(0);
        listA = push(listA, 1);
        listA = push(listA, 2);
        listA = push(listA, 3);
        listA = push(listA, 4);

        List listB = listA;

        System.out.println("Input List A : " + listA.toString());
        System.out.println("Input List B : " + listB.toString());
        listA = appendLists(listA, listB);
        System.out.println("Combined Input Lists A and B : " + listA.toString());
    }
}

You don't have 2 lists. 您没有2个列表。 You only have one. 你只有一个。

   List listB = listA;

assigns the reference listB to point to listA . 将引用listB指定为指向listA So you're appending listA onto itself. 所以你要将listA附加到自身上。

Regardless of what other issues you have, I would correct this (the simplest way being to create a listB in a similar fashion to how you've created listA ). 不管你有什么其他问题,我会纠正这个(是创建一个最简单的方法listB以类似的方式向你如何创建listA )。

My other comment (hope you don't mind) is that you've created a List object, but it has little/no behaviour of its own. 我的另一个评论(希望你不介意)是你已经创建了一个List对象,但它有很少/没有自己的行为。 Instead of creating your AppendLinkedLists class, I would put the functionality into the List object eg instead of: 我将功能放入List对象而不是创建您的AppendLinkedLists类,而不是:

listA = push(listA, 1);

write: 写:

listA.push(1);

etc. So the behaviour is encapsulated within the List object . 因此, 行为被封装在 List 对象中 Similarly you could then write: 同样,你可以写:

listA.push(listB);

by making use of overloading . 通过使用重载

I dint see the append code logic.. 我看到附加代码逻辑..

But your problem is 但你的问题是

List listB=listA; 列表listB = listA;

this is not doing what you are expecting it to , create a new listB. 这不是你所期望的,创建一个新的listB。 and copy contents of listA to listB. 并将listA的内容复制到listB。 Rather what it is doing is, create a new listB and copy the reference ID of listA to listB. 而是它正在做的是,创建一个新的listB并将listA的引用ID复制到listB。 ie there is only one list , listA. 即只有一个列表,listA。

correct this first 先纠正这个

You said you have 2 lists namely listA and listB , But you have called new only once. 你说你有2所列出即listAlistB ,但你有人称new一次。

List listA = new List(0); 

it will create a new list and listA will point to it. 它将创建一个新列表, listA将指向它。

List listB = listA; 

it will make listB point to same list, to which listA is pointing. 它将使listB指向同一个列表,到listA指向。

So, if you want to create another list also, create in same way how you created listA 因此,如果您还想创建另一个列表,请以相同的方式创建listA创建listA

You're in an inifinite loop, because listA is the same list as listB - toString will never end, because you've made a cycle. 你处于一个无限循环中,因为listA与listB是相同的列表 - toString将永远不会结束,因为你已经创建了一个循环。 In fact (imaginary) field listA.End.next would point to listA.Start. 实际上(虚构的)字段listA.End.next将指向listA.Start。

You have to either recreate listB or actually copy listA. 您必须重新创建listB或实际复制listA。

I think the problem you are facing is that ListA and ListB are actually one and the same list, so if you modify one, you modify the other as well. 我认为你面临的问题是ListA和ListB实际上是同一个列表,所以如果修改一个,你也可以修改另一个。 So when you append ListB to ListA, you are actually appending ListA to ListA, which leads to an infinite list. 因此,当您将ListB附加到ListA时,实际上是将ListA附加到ListA,这会导致无限列表。

The problem is in this line: 问题出在这一行:

List listB = listA;

What did does is that it assigns a second pointer (ListB) to the same list that ListA is pointing to. 它做的是它将第二个指针(ListB)分配给ListA指向的同一个列表。

To fix it you will need to make an actual copy of ListA. 要修复它,您需要制作ListA的实际副本。 Given you are trying to test your skills I will leave the exact "how" as an exercise ;) 鉴于你正试图测试你的技能,我会留下确切的“如何”作为练习;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM