Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Merge Sorted Linked List

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Merge Sorted Linked List

    I want to merge two sorted LinkedList from initialized LinkedList (not made from scratch - see code below). After browsing online, every implementation suggested is for LinkedList made from scratch where you define a class Node, etc.. etc. So it doesn't help? Do you have any suggestions on merging two linked lists from my code below?

     LinkedList<Integer> list = new LinkedList<>();
            LinkedList<Integer> list2 = new LinkedList<>();
     
            list.add(1);
            list.add(3);
            list.add(7);
            list.add(9);
            list.add(13);
     
            list2.add(2);
            list2.add(40);


    --- Update ---

    UPDATE - I got to use the method addAll() to add both list to another list and then sort it via Collections.sort() - In another words, it works

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Merge Sorted Linked List

    use the method addAll() to add both list to another list and then sort it via Collections.sort()
    Have you tried that? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    siid14 (July 1st, 2022)

Similar Threads

  1. [SOLVED] lookup method in a sorted linked list
    By arhzz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 16th, 2020, 01:37 PM
  2. Sort and Merge Two Linked List
    By Loraeron in forum What's Wrong With My Code?
    Replies: 49
    Last Post: February 28th, 2013, 10:08 PM
  3. Java Newbie - Sorted Linked List not inserting properly - please help!
    By bubbleboy in forum What's Wrong With My Code?
    Replies: 20
    Last Post: June 17th, 2011, 11:48 AM
  4. Sorted Doubly linked List
    By student in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 09:52 PM
  5. Merge 2 Sorted Text Files into a Third
    By Epyllion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 08:24 PM

Tags for this Thread