-
题目描述:
-
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
(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
- /*********************************
- * 日期:2013-11-23
- * 作者:SJF0115
- * 题号: 题目1519:合并两个排序的链表
- * 来源:http://ac.jobdu.com/problem.php?pid=1519
- * 结果:AC
- * 来源:剑指Offer
- * 总结:
- **********************************/
- #include<iostream>
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- using namespace std;
- typedef struct ListNode{
- int value;
- struct ListNode* next;
- }ListNode;
- //打印链表
- void OutPut(ListNode*head){
- if(head == NULL){
- printf("NULL\n");
- }
- else{
- ListNode *p;
- p = head->next;
- while(p != NULL){
- //如果是最一个
- if(p->next == NULL){
- printf("%d\n",p->value);
- }
- else{
- printf("%d ",p->value);
- }
- p = p->next;
- }
- }
- }
- //创建链表
- 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带有头结点
- if(head1->next == NULL && head2->next == NULL){
- return NULL;
- }
- //只有第一个字符串,无需合并,直接输出
- else if(head2->next == NULL){
- return head1;
- }
- //只有第二个字符串,无需合并,直接输出
- else if(head1->next == NULL){
- return head2;
- }
- //合并
- 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没有遍历完
- while(p1 != NULL){
- p3->next = p1;
- p1 = p1->next;
- p3 = p3->next;
- }
- //head2没有遍历完
- 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;
- //创建链表
- if(n != 0){
- head1 = CreateList(head1,n);
- }
- if(m != 0){
- head2 = CreateList(head2,m);
- }
- //合并排序
- head1 = Merge(head1,head2);
- //打印链表
- if(head1 == NULL){
- printf("NULL\n");
- }
- else{
- OutPut(head1);
- }
- }//while
- return 0;
- }
【方法二】
【解析】
【代码】
- /*********************************
- * 日期:2013-11-23
- * 作者:SJF0115
- * 题号: 题目1519:合并两个排序的链表
- * 来源:http://ac.jobdu.com/problem.php?pid=1519
- * 结果:AC
- * 来源:剑指Offer
- * 总结:
- **********************************/
- #include<iostream>
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- using namespace std;
- typedef struct ListNode{
- int value;
- struct ListNode* next;
- }ListNode;
- //打印链表
- void OutPut(ListNode*head){
- if(head == NULL){
- printf("NULL\n");
- }
- else{
- ListNode *p;
- p = head;
- while(p != NULL){
- //如果是最一个
- if(p->next == NULL){
- printf("%d\n",p->value);
- }
- else{
- printf("%d ",p->value);
- }
- p = p->next;
- }
- }
- }
- //创建链表
- 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){
- if(head1 == NULL && head2 == NULL){
- return NULL;
- }
- else if(head2 == NULL){
- return head1;
- }
- else if(head1 == NULL){
- return head2;
- }
- //合并
- 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;
- //创建链表
- if(n != 0){
- head1 = CreateList(head1,n);
- }
- if(m != 0){
- head2 = CreateList(head2,m);
- }
- //合并排序
- head1 = Merge(head1->next,head2->next);
- //打印链表
- if(head1 == NULL){
- printf("NULL\n");
- }
- else{
- OutPut(head1);
- }
- }//while
- return 0;
- }