Append two singly linked list
Append two singly linked list
I am trying to implement LinkedList using Java, just to test my skills. 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 :
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()); } }
Answer by Brian Agnew for Append two singly linked list
You don't have 2 lists. You only have one.
List listB = listA;
assigns the reference listB
to point to listA
. So you're appending listA
onto itself.
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
).
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. Instead of creating your AppendLinkedLists
class, I would put the functionality into the List
object e.g. instead of:
listA = push(listA, 1);
write:
listA.push(1);
etc. So the behaviour is encapsulated within the List
object. Similarly you could then write:
listA.push(listB);
by making use of overloading.
Answer by Piotr Zierhoffer for Append two singly linked list
You're in an inifinite loop, because listA is the same list as listB - toString will never end, because you've made a cycle. In fact (imaginary) field listA.End.next would point to listA.Start.
You have to either recreate listB or actually copy listA.
Answer by arendjr for Append two singly linked list
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. So when you append ListB to ListA, you are actually appending ListA to ListA, which leads to an infinite list.
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.
To fix it you will need to make an actual copy of ListA. Given you are trying to test your skills I will leave the exact "how" as an exercise ;)
Answer by Mukul Goel for Append two singly linked list
I dint see the append code logic..
But your problem is
List listB=listA;
this is not doing what you are expecting it to , create a new listB. and copy contents of listA to listB. Rather what it is doing is, create a new listB and copy the reference ID of listA to listB. i.e. there is only one list , listA.
correct this first
Answer by Azodious for Append two singly linked list
You said you have 2 lists namely listA
and listB
, But you have 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 same list, to which listA
is pointing.
So, if you want to create another list also, create in same way how you created listA
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment