Add two separately linked lists

I am trying to implement LinkedList using Java, just to test my skills. I am stuck in one problem where I need to add two linked lists that I created. I get into an endless cycle. Is there a way to improve the code and implement the desired result?

I / Ps:

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

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

O / P should be: 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()); } } 
+4
source share
5 answers

You do not have 2 listings. You only have one.

  List listB = listA; 

assigns a listB reference to point to listA . This way you add listA to yourself.

No matter what other problems you have, I would fix it (the easiest way to create listB same as you created listA ).

My other comment (hope you don't mind) is that you created a List object, but it has little / no native behavior. Instead of creating the AppendLinkedLists class, I would add functionality to the List object, for example. instead:

 listA = push(listA, 1); 

records:

 listA.push(1); 

etc .. Thus, the behavior is encapsulated inside the List object. Similarly, you could write:

 listA.push(listB); 

using overload.

+5
source

I do not see the logic of adding code.

But your problem

List listB = listA;

this does not do what you expect, create a new listB. and copy the contents of list A to listB. Most likely it creates a new listB and copies the linkA identifier listA to listB. that is, there is only one list, listA.

fix this first

+2
source

You said that you have 2 lists, namely listA and listB , but you called new only once.

 List listA = new List(0); 

it will create a new list, and listA will point to it.

 List listB = listA; 

it will make listB point to the same list that listA points listA .

So, if you want to create another list, create the same way you created listA

+1
source

You are in a closed loop because listA is the same list as listB - toString will never end because you made a loop. In fact, the (imaginary) field listA.End.next points to listA.Start.

You need to either recreate listB or actually copy listA.

0
source

I think the problem you are facing is that ListA and ListB are actually the same list, so if you change it, you also change the other. So when you add ListB to ListA, you actually add ListA to ListA, which leads to an infinite list.

The problem is in this line:

 List listB = listA; 

What it does is it assigns to the second pointer (ListB) the same list that ListA points to.

To fix this, you will need to make an actual copy of ListA. Given that you are trying to test your skills, I will leave the exact “how” as an exercise;)

0
source

Source: https://habr.com/ru/post/1436571/


All Articles