Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

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

John Wild
Greenhorn
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
Posts: 50702
83
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.