Would the exercice be easier if it allows to create new node ? |
No.
I'm having a really hard time getting through this logic. What exactly does IntNode::SetLink() do? Are these nodes singly linked or doubly linked?
EDIT: Well, I give up. I'm left with this at the end of the first while:
http://imageshack.us/photo/my-images/842/clipboard01k.png/
Even without knowing those things I just asked, I'm fairly sure your logic is completely broken and will definitely leave you with malformed lists at the end. Start over.
Additionally, I strongly suggest not to do this:
1 2
|
head1 = next1;
next1 = next1->getLink();
|
And to instead do this:
|
head1 = head1->getLink();
|
EDIT 2: The main problem is that you're treating the merge operation as a sort of intertwined insertion between two lists.
There's basically two ways to merge lists (that I can think of ATM):
* Breaking the lists apart and reconstructing them through this process:
1 2 3 4 5 6 7 8 9 10 11 12
|
//[] is the empty list
//a:[] = [a]
//a:b:[] = a:(b:[]) = [a, b]
//a:b:c:[] = a:(b:(c:[])) = [a, b, c]
//head() returns the first node of the list
//tail() returns all the other nodes
merge(a,[]) = a
merge([],a) = a
merge(a,b) = if head(a)<head(b)
then (head(a):merge(tail(a),b))
else (head(b):merge(a,tail(b)))
|
* Insert-sorted each node of one list into the other list, but
the list that gets nodes inserted into it and the list whose nodes are inserted don't swap responsibilities.
You would appear to be trying some weird combination of the two, which of course doesn't work.