7-2 两个有序链表序列的合并(C语言/C++解法)(PTA)

题目描述

已知两个非降序链表序列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

解题思路

对于C语言,就是很经典的链表合并算法,多练多理解.C++的解法很巧妙,也是利用了C++的vector的特性,甚至连结构体都不用开

C语言解法

#include <stdio.h>
#include <stdlib.h>
typedef struct LinkList
{
    int data;
    struct LinkList* next;
}link;

link* CreateandPush() //封装了节点创建以及尾插
{
    link* phead, *cur, *newNode;
    int n;
    phead = (link*)malloc(sizeof(struct LinkList));
    phead->next = NULL;
    cur = phead;
    while (scanf("%d", &n) && n != -1)//控制输入的数据到-1截止
    {
        // 创建新节点
        newNode = (link*)malloc(sizeof(struct LinkList));
        newNode->data = n;
        newNode->next = NULL;

        //cur追踪尾部节点
        cur->next = newNode;
        cur = newNode;
    }
    return phead;
}
link* Union(link* head1,link* head2)
{
    link* pointA, *pointB, *assistPoint;

    // 处理空链表的情况
    if (head1 == NULL && head2 == NULL)
        return NULL;
    else if (head1 == NULL && head2 != NULL)//如果链表1为空 那么不用执行合并操作
        return head2;
    else if (head1 != NULL && head2 == NULL)//如果链表2为空 那么不用执行合并操作
        return head1;

    pointA = head1->next; // 用于遍历head1链表
    pointB = head2->next; // 用于遍历head2链表
    assistPoint = head1; //pc是一个辅助指针,用于连接合并后的链表节点
    free(head2);

    while (pointA && pointB) // 循环遍历两个链表,直到其中一个链表遍历完为止
    {
        // 根据从小到大的顺序排列
        if (pointA->data <= pointB->data)  //如果pA的数据小于或等于pB的数据 
        {
            assistPoint->next = pointA; // 将pA节点连接到合并后的链表上
            assistPoint = pointA; // 移动assistPoint指针为pA的位置(aP追踪链表末尾位置)
            pointA = pointA->next; // pA持续遍历head1链表
        }
        else // 如果pA的数据大于pB的数据
        {
            assistPoint->next = pointB; // 将pB节点连接到合并后的链表上
            assistPoint = pointB; // 移动aP指针为pB的位置(pc追踪链表末尾位置)
            pointB = pointB->next; // pB持续遍历head2链表
        }
    }

    //处理剩余的结点,不用while(pointA)是因为PointA实际上也是一个结构体 说白了就是一个链表 只是把剩余的链表连起来而已
    if (pointA) 
    {
        assistPoint->next = pointA;
    }
    if (pointB)
    {
        assistPoint->next = pointB;
    }
    return head1; //返回head1而不是aP是因为aP经过不断移动此时已经不是链表的起始位置了
}
void Print(link* head1)
{
    link* q;
    q = head1->next;
    while (q != NULL)
    {
        if (q->next == NULL) //如果是链表末尾
        {
            printf("%d\n", q->data);
        }
        else
        {
            printf("%d ", q->data);
        }
        q = q->next;
    }
}
int main()
{
    link* list1, *list2;
    list1 = CreateandPush();
    list2 = CreateandPush();
    list1 = Union(list1, list2);
    if (list1->next == NULL)//list1->next==NULL;
        printf("NULL\n");//如果为空链表就输出NULL;
    else
        Print(list1);
    return 0;
}

C++解法

#include <bits/stdc++.h>
using namespace std;

vector<int>a, b, c;

int main()
{
	int num;
	while (cin >> num && num != -1)
	{
		a.push_back(num);
	}
	while (cin >> num && num != -1)
	{
		b.push_back(num);
	}

	auto it1 = a.begin(), it2 = b.begin();
	while (it1 != a.end() && it2 != b.end())
	{
		if (*it1 < *it2)
		{
			c.push_back(*it1);
			it1++;
		}
		else
		{
			c.push_back(*it2);
			it2++;

		}
	}
	//题目保证输入有序
	while (it1!=a.end())
	{
		c.push_back(*it1);
		it1++;
	}
	while (it2 != b.end())
	{
		c.push_back(*it2);
		it2++;
	}

	bool isFirst = true; // 标记第一个元素以实现结尾无空格
	for (auto element : c)
	{
		if (!isFirst)
		{
			cout << " ";
		}
		cout << element;
		isFirst = false;
	}
    if(c.empty()) //若新链表为空,输出NULL
    {
        cout<<"NULL"<<endl;
    }
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值