两个有序链表序列的合并

两个有序链表序列的合并

题目描述
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。

输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:
在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。

输入样例:
1 3 5 -1
2 4 6 8 10 -1
输出样例:
1 2 3 4 5 6 8 10

一开始我直接上手做!咔咔咔

#include<iostream>
using namespace std;
int a[1000001],b[1000001];
int s[2000002];//不知道要多大,我乱设置大小 
int main()
{
	int stop1,stop2;//标志着结束 
	int j,k=0;
	int head1=0,head2=0;//标志着...头结点?(应该可以这么叫吧 
	for(int i=0;i<10001;i++)
	{
		cin>>a[i];
		stop1=i;//标记最尾端 
		if(a[i]==-1)
		break;
	}
	for(int i=0;i<10001;i++)
	{
		cin>>b[i];
		stop2=i;//也是 
		if(b[i]==-1)
		break;
	}
	if(a[0]!=-1&&b[0]!=-1)
	{
		for(j=1;j<=stop1+stop2;j++)
		{
			if(a[head1]<b[head2])
			{
				s[k]=a[head1];//小的入到数组s里 
				k++;
				head1++;
			}
			else
			{
				s[k]=b[head2];
				k++;
				head2++;
			}
			if(head1==stop1||head2==stop2)
			{
				break;//又一个数组完了就跳出比较 
			}
		}
		
		if(j<stop1+stop2)//剩下的那个数组一顿输出就完事了 
		{
			if(head1==stop1)
			{
				for(int i=head2;i<stop2;i++)
				{
					s[k]=b[i];
					k++;
				}
			}
			else
			{
				for(int i=head1;i<stop1;i++)
				{
					s[k]=a[i];
					k++;
				}
			}
		}
		for(int i=0;i<stop1+stop2;i++)
		{
			if(i==stop1+stop2-1)
			cout<<s[i];
			else
			cout<<s[i]<<" ";
		}
	}
	else
	{
		cout<<"NULL"<<endl;
	}
	return 0;
}

在PTA上提交部分正确
非常非常啰嗦,还错了,啧啧啧,好是悲伤呢。

后来我改了一点,还是错了,害

#include<iostream>
using namespace std;
int a[1000005];
int b[1000005];
int s[2000005];//不知道要多大,我乱设置大小 
int main()
{
	int stop1,stop2;//标志着结束 
	int j,k=0;
	int head1=0,head2=0;//标志着...头结点?(应该可以这么叫吧 
	for(int i=0;i<1000005;i++)
	{
		cin>>a[i];
		stop1=i;//标记最尾端 
		if(a[i]==-1)
		break;
	}
	for(int i=0;i<1000005;i++)
	{
		cin>>b[i];
		stop2=i;//也是 
		if(b[i]==-1)
		break;
	}
	stop1--;
	stop2--;
    if(stop1==-1&&stop2==-1)
    {
        cout<<"NULL"<<endl;
    }
    else if(stop1==-1)
    {
    	for(int i=0;i<=stop2;i++)
		{
			if(i==stop2)
			cout<<b[i]<<endl;
			else
			cout<<b[i]<<" ";
		}
	}
	else if(stop2==-1)
	{
		for(int i=0;i<=stop1;i++)
		{
			if(i==stop1)
			cout<<a[i]<<endl;
			else
			cout<<a[i]<<" ";
		}
	}
	else
	{
		for(j=0;j<=stop1+stop2+1;j++)
		{
			if(a[head1]<b[head2])
			{
				s[k]=a[head1];//小的入到数组s里
				k++;
				head1++;
			}
			else
			{
				s[k]=b[head2];
				k++;
				head2++;
			}
			if(head1==stop1+1||head2==stop2+1)
			{
				break;//又一个数组完了就跳出比较 
			}
		}
		if(j<stop1+stop2+1)//剩下的那个数组一顿输出就完事了 
		{
			if(head1==stop1+1)
			{
				for(int i=head2;i<stop2+1;i++)
				{
					s[k]=b[i];
					k++;
				}
			}
			else
			{
				for(int i=head1;i<stop1+1;i++)
				{
					s[k]=a[i];
					k++;
				}
			}
		}
		for(int i=0;i<stop1+stop2+2;i++)
		{
			if(i==stop1+stop2+1)
			cout<<s[i]<<endl;
			else
			cout<<s[i]<<" ";
		}
	}
	return 0;
}

然后我要开始观摩别人的代码了,然后我观摩了这篇的代码PAT 两个有序链表序列的合并

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值