02-linear structure 1 merge the sequence of two ordered linked lists, 02-linear

Source: Internet
Author: User

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.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.