标签:c style class blog code java
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
(hint: 请务必使用链表。)
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为两个整数n和m(0<=n<=1000,
0<=m<=1000):n代表将要输入的第一个链表的元素的个数,m代表将要输入的第二个链表的元素的个数。
下面一行包括n个数t(1<=t<=1000000):代表链表一中的元素。接下来一行包含m个元素,s(1<=t<=1000000)。
对应每个测试案例,
若有结果,输出相应的链表。否则,输出NULL。
5 2 1 3 5 7 9 2 4 0 0
1 2 3 4 5 7 9 NULL
if(n1 == 0 && n2 == 0) printf("NULL\n");
if(head1->next == NULL){ res->next = head2->next; return ; } if(head2->next == NULL){ res->next = head1->next; return ; }
Node *p1; Node *p2; Node *p3 = res; while(head1->next != NULL && head2->next != NULL){ p1 = head1->next; p2 = head2->next; if(p1->number < p2->number){ head1->next = p1->next; p1->next = p3->next; p3->next = p1; p3 = p3->next; }else{ head2->next = p2->next; p2->next = p3->next; p3->next = p2; p3 = p3->next; } } if(head1->next == NULL) p3->next = head2->next; if(head2->next == NULL) p3->next = head1->next;
#include <stdio.h> #include <stdlib.h> typedef struct node{ int number; struct node * next; }Node; void mergeList(Node *res,Node *head1,Node *head2); int main(){ int n1,n2; int i; int temp; while(scanf("%d %d",&n1,&n2)!=EOF && n1>=0 && n1<=1000 && n2>=0 && n2<=1000){ Node *head1 = (Node *)malloc(sizeof(Node)); Node *head2 = (Node *)malloc(sizeof(Node)); head1->next = NULL; head2->next = NULL; Node *tail1 = head1; Node *tail2 = head2; for(i=0;i<n1;i++){ scanf("%d",&temp); Node *p = (Node *)malloc(sizeof(Node)); p->next = tail1->next; tail1->next = p; p->number = temp; tail1 = tail1->next; } for(i=0;i<n2;i++){ scanf("%d",&temp); Node *p = (Node *)malloc(sizeof(Node)); p->next = tail2->next; tail2->next = p; p->number = temp; tail2 = tail2->next; } Node *res = (Node *)malloc(sizeof(Node)); mergeList(res,head1,head2); if(n1 == 0 && n2 == 0) printf("NULL\n"); else{ Node *p = res->next; printf("%d",p->number); p = p->next; while(p != NULL){ printf(" %d",p->number); p = p->next; } printf("\n"); } } return 0; } void mergeList(Node *res,Node *head1,Node *head2){ if(head1->next == NULL){ res->next = head2->next; return ; } if(head2->next == NULL){ res->next = head1->next; return ; } Node *p1; Node *p2; Node *p3 = res; while(head1->next != NULL && head2->next != NULL){ p1 = head1->next; p2 = head2->next; if(p1->number < p2->number){ head1->next = p1->next; p1->next = p3->next; p3->next = p1; p3 = p3->next; }else{ head2->next = p2->next; p2->next = p3->next; p3->next = p2; p3 = p3->next; } } if(head1->next == NULL) p3->next = head2->next; if(head2->next == NULL) p3->next = head1->next; return ; } /************************************************************** Problem: 1519 User: xhalo Language: C Result: Accepted Time:250 ms Memory:4212 kb ****************************************************************/
剑指OFFER之合并有序链表(九度OJ1519),布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/xing901022/p/3761780.html