• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the javadoc for LinkedList ?
you should be able to answer this and your other question after reading this.
 
Suna Dev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i use "toArray" ? it is correct or wrong?
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already been given an answer, but I would not call that merging two list. You are appending one list to the other.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!