02-linear structure 1 merge the sequence of two ordered linked lists, 02-linear
A function is required to merge the incremental integer sequence represented by two linked lists into a non-decreasing integer sequence.
This question is more basic and mainly describes the basic operations of the linked list in C language. As long as the linked list is connected, it is difficult to figure out the comparison process and the relationship between the two sides. The key is to clarify the operations on the linked list.
Typedef struct Node *PtrToNode;
Struct Node {
ElementType Data; /* Store node data */
PtrToNode Next; /* Pointer to the next node */
};
Typedef PtrToNode List; /* Defines a single linked list type */
Here is the first time I learned to use a temporary pointer px (x indicates null or 1 or 2, the same below). Many other questions related to linked list operations have similar operations. In this case, you first need to create an empty linked list L as the final returned result value, and then define the three temporary pointers p, p1, and p2 respectively pointing to L and the incoming linked lists L1 and L2. The advantage of this is that no matter how you operate on one or more nodes in Lx, you only need to use p. Lx still points to the head of the linked list, which can be found in time if necessary.
After preparation, start the connection. The overall idea is to compare the size and connect the elements at the position indicated by p1 and p2, if one of them is empty, p1/2 is directly connected to the end of p; then, the Next domain of L1 and L2 is pointed to null (this is required for the question ). The reason is to point its Next to NULL is that there is a header node when the Lx is created, and the data field of this header node is empty, therefore, the first node that actually contains the relevant value is the original node (Lx-> Next) pointed to by the pointer.
It is helpful to draw a linked list to understand the structure of the linked list and its related operations.