• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i use "toArray" ? it is correct or wrong?
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74031
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic