One , Topic requirements

* Now there are 2 Single list , They're all in order （ from small to large ）, Now I want you to merge two single linked lists into 1 Single list , After the merger, the linked list is still in order
* for example ：
* list1 by 1,3,5
* list2 by 2,4,6
* take list1 and list2 Merge into new linked list list3, The value is 1,2,3,4,5,6
Two , Coding implementation
/** * @description: take temp Node inserted into head In the linked list * @param: head: The starting position of the linked list to insert temp:
The node pointer of the linked list to insert * @return: Returns the head node of the inserted linked list * @author: Dongshao */ Node
*insert_node(Node *head, Node *temp) { Node *p = head; Node *q = NULL; //
as long as q No p The value of the node is small , Then go back and forth while(p->_data < temp->_data && p != NULL) { q = p; p =
p->_pNext; } // If p be equal to head, explain temp than head All the nodes in the linked list are small , So will temp Insert into head before ,
Direct return temp That's fine if(p == head) { temp->_pNext = p; return temp; } //
Otherwise, it will temp Insert into head In the middle of the linked list q->_pNext = temp; temp->_pNext = p; return head; } /**
* @description: Merge two ordered linked lists ( from small to large ) * @param : list1: The first 1 Linked list list2: The first 2 Linked list * @return:
Returns the head node of the merged linked list * @author: Dongshao */ Node *merge2List(Node *list1, Node *list2) {
// If which list is empty , Then return to another linked list if (list1 == NULL) return list2; if (list2 == NULL)
Direction and p Node after // Linked list 1 Comparison list 2 long if (listLength(list1) >= listLength(list2)) { head =
list1->_pNext; p = list2->_pNext; } else { head = list2->_pNext; p =
list1->_pNext; } // Which list is short , Just loop which short list to insert into the long list while (p != NULL) { q = p->_pNext;
// Save next node first head = insert_node(head, p); // take p Insert into head In the linked list p = q; } //
* @return: Returns the length of the linked list * @author: Dongshao */ size_t listLength(Node *_head) { if
(_head == NULL || _head->_pNext == NULL) return 0; size_t count; Node *temp =
_head->_pNext; while (temp != NULL) { count++; temp = temp->_pNext; } return
count; }
Three , test

* The test code is as follows #include <stdio.h> #include <stdlib.h> typedef struct node { struct
node *_pNext; int _data; } Node; Node *insert_node(Node *head, Node *temp);
Node *merge2List(Node *list1, Node *list2); Node *createList(int num); void
temp: The node pointer of the linked list to insert * @return: Returns the head node of the inserted linked list * @author: Dongshao */ Node
*insert_node(Node *head, Node *temp) { Node *p = head; Node *q = NULL; //
as long as q No p The value of the node is small , Then go back and forth while(p->_data < temp->_data && p != NULL) { q = p; p =
p->_pNext; } // If p be equal to head, explain temp than head All the nodes in the linked list are small , So will temp Insert into head before ,
Direct return temp That's fine if(p == head) { temp->_pNext = p; return temp; } //
Otherwise, it will temp Insert into head In the middle of the linked list q->_pNext = temp; temp->_pNext = p; return head; } /**
* @description: Merge two ordered linked lists ( from small to large ) * @param : list1: The first 1 Linked list list2: The first 2 Linked list * @return:
Returns the head node of the merged linked list * @author: Dongshao */ Node *merge2List(Node *list1, Node *list2) {
// If which list is empty , Then return to another linked list if (list1 == NULL) return list2; if (list2 == NULL)
Direction and p Node after // Linked list 1 Comparison list 2 long if (listLength(list1) >= listLength(list2)) { head =
list1->_pNext; p = list2->_pNext; } else { head = list2->_pNext; p =
list1->_pNext; } // Which list is short , Just loop which short list to insert into the long list while (p != NULL) { q = p->_pNext;
// Save next node first head = insert_node(head, p); // take p Insert into head In the linked list p = q; } //
Returns the head node of the newly formed linked list return head; } /** * @description: Create linked list * @param: num: Number of nodes in linked list *
@return: Head pointer to create a new linked list * @author: Dongshao */ Node *createList(int num) { if (num <
NULL; Node *_tempHead = _head; Node *_newNode; for (int i = 0; i < num; ++i) {
// Create a new node _newNode = (Node *)malloc(sizeof(Node)); _newNode->_data = (i + 1);
_newNode; _newNode = NULL; } return _head; } /** * @description: Print linked list * @param:
if (_head == NULL) return; Node *_tempNode = _head; // Circular printing while (_tempNode !=
NULL) { if (_tempNode->_pNext == NULL) break; else printf("%d->",
_tempNode->_data); _tempNode = _tempNode->_pNext; } // Print the printf("%d\n",
@return: Returns the length of the linked list * @author: Dongshao */ size_t listLength(Node *_head) { if
(_head == NULL || _head->_pNext == NULL) return 0; size_t count; Node *temp =
_head->_pNext; while (temp != NULL) { count++; temp = temp->_pNext; } return
@author: Dongshao */ void freeList(Node *_head) { if (_head == NULL ||