At the beggining there are standard tests for NULL parameters which corresponds to the basic case that one list is blank so the other is ordered by default.
The writter does a trick here, each time the next item will be added from the pointer to Node named list1, this happends in the line: *pnext = list1;
, whats left is to understand how is it possible to always add from one list, this is done using the SWAP_PTRS macro which plants the list startig with the lower value at list1
.
After an update has accured the next Node which should be updated is found using: pnext = &list1->next;
. the reason it works is also interesting, since the current updated node has pointed to the current item of list1, the value in its next field is the pointer to the location which should be updated next, by passing the address of this pointer (using &) we are able to change the content of this next item.
This process goes on and on until the item added was the last one on a list. An interesting point here is that there is no edge condition since the removed items are always from list1 which means that list2 cant be empty before list1. When the last item of list1 is added the while loop terminates and the rest of list2 is added to the end of the result list.