This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem Merging 2 sorted linked lists of numbers in order from least to greatest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem Merging 2 sorted linked lists of numbers in order from least to greatest" Watch "Problem Merging 2 sorted linked lists of numbers in order from least to greatest" New topic
Author

Problem Merging 2 sorted linked lists of numbers in order from least to greatest

John Wild
Greenhorn

Joined: Mar 27, 2013
Posts: 1
I am having a problem with the last step of merging 2 sorted linked lists in the correct order I know my logic is off somewhere but not having much luck


Here is my ouput

Before:
{5}{10}{2}{2}{9}{8}
List after:
{2}{2}{5}{8}{9}{10}
Before:
{9}{2}{5}{9}{4}
List after:
{2}{4}{5}{9}{9}
Merged:
{10}{9}{8}{5}{2}{9}{9}{5}{4}{2}{2} ----I am trying to figure out how to correct this problem
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Welcome to the Ranch

You cannot usually sort out such an error by examining the code. You work it out on paper. Imagine you have two lists like that, and you want to merge them. Draw a diagram of those lists, and show how you intend to move their head nodes to the tail of a third list. Or maybe you should remove the head values and add them to the tail of the new List. You would appear to be sorting the Lists all right (but read this about sorting), so you probably only have to work on that one stage.

Remember: pencil paper and eraser. That last is the most important piece of equipment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem Merging 2 sorted linked lists of numbers in order from least to greatest
 
Similar Threads
Simple ArrayList conundrum?
cannot insert into linkedlist after delete
test bed for array based list implementation
Insert into Doubly LinkedList
Linked List Sorting Problem