Q: Input two monotonically increasing linked lists, and output the linked list after combining the two linked lists. Of course, we need the combined linked list to satisfy the monotonically non-decreasing rule.
A: Select the linked list with the smaller element value of the first node as the basic linked list, and link the nodes in the other linked list to the basic linked list by constantly comparing the element values of the two linked lists.
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { if(pHead1 == NULL && pHead2 == NULL) { return NULL; } if(pHead1 != NULL && pHead2 == NULL) { return pHead1; } if(pHead1 == NULL && pHead2 != NULL) { return pHead2; } // ListNode* tmp = new ListNode(0); ListNode* p1 = pHead1; ListNode* q1 = pHead1; ListNode* p2 = pHead2; ListNode* q2 = pHead2; if(pHead1->val <= pHead2->val) { while(p1 && p2) { q2 = p2; q1 = p1; p2 = p2->next; p1 = p1->next; q2->next = q1->next; q1->next = q2; } return pHead1; } else { while(p1 && p2) { q1 = p1; q2 = p2; p1 = p1->next; p2 = p2->next; q1->next = q2->next; q2->next = q1; } return pHead2; } } };
Reference method 1:
#include<iostream> #include <stdio.h> #include <malloc.h> #include <string.h> using namespace std; typedef struct ListNode{ int value; struct ListNode* next; }ListNode; //print linked list void OutPut(ListNode*head){ if(head == NULL){ printf("NULL\n"); } else{ ListNode *p; p = head->next; while(p != NULL){ //if it is the first if(p->next == NULL){ printf("%d\n",p->value); } else{ printf("%d ",p->value); } p = p->next; } } } //create linked list ListNode* CreateList(ListNode *head,int n){ ListNode *newNode,*p; p = head; for(int i = 0;i < n;i++){ newNode = (ListNode*)malloc(sizeof(ListNode)); scanf("%d",&newNode->value); newNode->next = NULL; p->next = newNode; p = newNode; } return head; } ListNode* Merge(ListNode*head1,ListNode*head2){ //head1, head2 with head node if(head1->next == NULL && head2->next == NULL){ return NULL; } //Only the first string, no need to merge, output directly else if(head2->next == NULL){ return head1; } //Only the second string, no need to merge, output directly else if(head1->next == NULL){ return head2; } //merge else{ ListNode *p1,*p2,*p3,*head; head = (ListNode*)malloc(sizeof(ListNode)); head->next = NULL; p1 = head1->next; p2 = head2->next; p3 = head; while(p1 != NULL && p2 != NULL){ if(p1->value < p2->value){ p3->next = p1; p1 = p1->next; } else{ p3->next = p2; p2 = p2->next; } p3 = p3->next; } //head1 has not been traversed while(p1 != NULL){ p3->next = p1; p1 = p1->next; p3 = p3->next; } //head2 has not finished traversing while(p2 != NULL){ p3->next = p2; p2 = p2->next; p3 = p3->next; } return head; } } int main() { int i,n,m; while(scanf("%d %d",&n,&m) != EOF){ ListNode *head1,*head2; head1 = (ListNode*)malloc(sizeof(ListNode)); head2 = (ListNode*)malloc(sizeof(ListNode)); head1->next = NULL; head2->next = NULL; //create linked list if(n != 0){ head1 = CreateList(head1,n); } if(m != 0){ head2 = CreateList(head2,m); } // merge sort head1 = Merge(head1,head2); //print linked list if(head1 == NULL){ printf("NULL\n"); } else{ OutPut(head1); } }//while return 0; }
Reference method 2:
#include<iostream> #include <stdio.h> #include <malloc.h> #include <string.h> using namespace std; typedef struct ListNode{ int value; struct ListNode* next; }ListNode; //print linked list void OutPut(ListNode*head){ if(head == NULL){ printf("NULL\n"); } else{ ListNode *p; p = head; while(p != NULL){ //if it is the first if(p->next == NULL){ printf("%d\n",p->value); } else{ printf("%d ",p->value); } p = p->next; } } } //create linked list ListNode* CreateList(ListNode *head,int n){ ListNode *newNode,*p; p = head; for(int i = 0;i < n;i++){ newNode = (ListNode*)malloc(sizeof(ListNode)); scanf("%d",&newNode->value); newNode->next = NULL; p->next = newNode; p = newNode; } return head; } //recursive ListNode* Merge(ListNode*head1,ListNode*head2){ if(head1 == NULL && head2 == NULL){ return NULL; } else if(head2 == NULL){ return head1; } else if(head1 == NULL){ return head2; } //merge ListNode *head = NULL; if(head1->value < head2->value){ head = head1; head->next = Merge(head1->next,head2); } else{ head = head2; head->next = Merge(head1,head2->next); } return head; } int main() { int i,n,m; while(scanf("%d %d",&n,&m) != EOF){ ListNode *head1,*head2; head1 = (ListNode*)malloc(sizeof(ListNode)); head2 = (ListNode*)malloc(sizeof(ListNode)); head1->next = NULL; head2->next = NULL; //create linked list if(n != 0){ head1 = CreateList(head1,n); } if(m != 0){ head2 = CreateList(head2,m); } // merge sort head1 = Merge(head1->next,head2->next); //print linked list if(head1 == NULL){ printf("NULL\n"); } else{ OutPut(head1); } }//while return 0; }