题目1519:合并两个排序的链表-九度

题目描述:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
(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

推荐指数:※

来源:http://ac.jobdu.com/problem.php?pid=1519

合并两个链表,在扫描过程中,操作节点,两个link合并到link1中。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct node{
    int val;
    node * next;
}node;
void input_link(node *head ,int len){
    node *pre=head;
    for(int i=0;i<len;i++){
        node *nnode=new node;
        nnode->next=NULL;
        scanf("%d",&nnode->val);
        pre->next=nnode;
        pre=nnode;
    }
}
node*  merge_link(node *head1,node *head2){
    node *p1=head1->next;
    node *pre1=head1;
    node *p2=head2->next;
    while(p1!=NULL&&p2!=NULL){
		if(p1->val<p2->val){
			pre1=p1;
			p1=p1->next;
		}
		else{
			node *q=p2->next;//store 
			p2->next=p1;//insert
			pre1->next=p2;//link
			pre1=p2;//finish
			p2=q;
		}
    }
	if(p2!=NULL)
		pre1->next=p2;
    return head1;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        node *head1=new node;
        node *head2=new node;
		head1->next=NULL;
		head2->next=NULL;
        input_link(head1,n);
        input_link(head2,m);
        node * p=merge_link(head1,head2);
        p=p->next;
        if(p!=NULL){
            printf("%d",p->val);
			while(p->next!=NULL){
				p=p->next;
				printf(" %d",p->val);
			}
		}
		else
			printf("NULL");
        printf("\n");
    }
    return 0;
}

海涛的递归算法。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct node{
    int val;
    node * next;
}node;
void input_link(node *head ,int len){
    node *pre=head;
    for(int i=0;i<len;i++){
        node *nnode=new node;
        nnode->next=NULL;
        scanf("%d",&nnode->val);
        pre->next=nnode;
        pre=nnode;
    }
}
node*  merge_link(node *head1,node *head2){
    if(head1==NULL)
        return head2;
    else if(head2==NULL)
        return head1;
    node *p=NULL;
    if(head1->val<head2->val){
        p=head1;
        p->next=merge_link(head1->next,head2);
    }
    else{
        p=head2;
        p->next=merge_link(head1,head2->next);
    }
    return p;
    
}


int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        node *head1=new node;
        node *head2=new node; 
        input_link(head1,n);
        input_link(head2,m);
        node *p= merge_link(head1->next,head2->next);
        if(p!=NULL){
            printf("%d",p->val);
            while(p->next!=NULL){
                p=p->next;
                printf(" %d",p->val);
            } 
        }
        else
            printf("NULL");
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值