Forums Register Login

How to merge the two Linked List into a new named (x and y = xy)

+Pie Number of slices to send: Send


that is the my own code. exactly i want to merge "link" to "link2", link3 = (A, B, C, D, E, F). And after that The reverse of the queue will be (F, E, D, C, B, A) so how to do that? please help me.
+Pie Number of slices to send: Send
 

Suna Dev wrote:
that is the my own code. exactly i want to merge "link" to "link2", link3 = (A, B, C, D, E, F).


Have you tried anything with respect to the task you have to do? I mean, not just creating and initializing the linked list, but merging them. What issues are you facing?
+Pie Number of slices to send: Send
Have you read the javadoc for LinkedList ?
You should be able to answer this and your other question after reading this.
+Pie Number of slices to send: Send
can i use "toArray" ? it is correct or wrong?
1
+Pie Number of slices to send: Send
I don't see how converting a list to an array could help with merging two lists.
Look at the LinkedList javadoc and check if there is something that could help you with your task.

BTW, if you are adding elements to a new list (or just want to add at the end of the list), you don't have to provide an index.
Writing link.add("A") would suffice.
+Pie Number of slices to send: Send
You have already been given an answer, but I would not call that merging two list. You are appending one list to the other.
+Pie Number of slices to send: Send
 

Suna Dev wrote:that is the my own code. exactly i want to merge "link" to "link2", link3 = (A, B, C, D, E, F).


It sounds to me like you're being far too literal about these things.

Merging is a generic problem, so you really shouldn't be worrying about whether the list is "linked" or not. The chances are that your solution should look something like:(don't forget that LinkedList ––IS-A→ List ––IS-A→ Collection. The output must be a List, because it has a specific order)

About the only thing that's missing from the above declaration is how you want to merge (ie, what defines the output order), and you can supply that with a Comparator (java.util.Comparator). So your final method might well look something like:indeed, one might ask why such a method wasn't included in java.util.Collections.

I'll leave you to think about how you might implement it.

And after that The reverse of the queue will be (F, E, D, C, B, A) so how to do that? please help me.


Reversal has nothing to do with merging. Assuming you have a "merged" List, you can easily gets its elements in reverse sequence by using its ListIterator.

HIH

Winston
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 765 times.
Similar Threads
browser back button problem.
Linked List need help
Creating a spell checker - guidance/ideas?
scjp1.4 qtn on Mult-dimensional array
print random letters(LinkedList)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:08:52.