1-7 两个有序链表序列的交集

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。

输入格式:

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

输出格式:

在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL

输入样例:

1 2 5 -1
2 4 5 8 10 -1

输出样例:

2 5

代码:(这个不全对):

#include<stdio.h>
#include<stdlib.h>

typedef struct List {
	int num;
	struct List* next;
}Node;

int main() {
	List* list1 = (List*)malloc(sizeof(struct List));
	List* list2 = (List*)malloc(sizeof(struct List));
	List* list3 = (List*)malloc(sizeof(struct List));
	list1->next = NULL;
	list2->next = NULL;

    int n;

	Node* curNode1 = list1;
	scanf("%d", &n);
	while (n != -1) {
		Node* newNode = (List*)malloc(sizeof(struct List));
		newNode->num = n;
		newNode->next = NULL;

		curNode1->next = newNode;
		curNode1 = newNode;

		scanf("%d", &n);
	}

	Node* curNode2 = list2;
	scanf("%d", &n);
	while (n != -1) {
		Node* newNode = (List*)malloc(sizeof(struct List));
		newNode->num = n;
		newNode->next = NULL;

		curNode2->next = newNode;
		curNode2 = newNode;

		scanf("%d", &n);
	}

	curNode1 = list1->next;
	curNode2 = list2->next;
	Node* curNode3 = list3;

	while (curNode1) {
		curNode2 = list2->next;
		while (curNode2) {
			if (curNode1->num == curNode2->num) {
				Node* newNode = (List*)malloc(sizeof(struct List));
				newNode->num = curNode1->num;
				newNode->next = NULL;

				curNode3->next = newNode;
				curNode3 = newNode;
			}
			curNode2 = curNode2->next;
		}
		curNode1 = curNode1->next;
	}

	int flag = 1;
	curNode3 = list3->next;
	if (!curNode3) {
		printf("NULL");
		return 0;
	}
	else {
		while (curNode3) {
			if (flag) {
				printf("%d", curNode3->num);
				flag = 0;
			}
			else {
				printf(" %d", curNode3->num);
			}
			curNode3 = curNode3->next;
		}
	}

}

该程序最后一个大规模数据测试点运行超时,因为使用了两重嵌套的循环:

while (curNode1) {
		curNode2 = list2->next;
		while (curNode2) {
			if (curNode1->num == curNode2->num) {
				Node* newNode = (List*)malloc(sizeof(struct List));
				newNode->num = curNode1->num;
				newNode->next = NULL;

				curNode3->next = newNode;
				curNode3 = newNode;
			}
			curNode2 = curNode2->next;
		}
		curNode1 = curNode1->next;
	}

因为题干中说到,“输入分两行,分别在每行给出由若干个正整数构成的非降序序列”,所以可以修改为:

while (curNode1 && curNode2) {
		if (curNode1->num < curNode2->num) {
			curNode1 = curNode1->next;
		}
		else if (curNode1->num > curNode2->num) {
			curNode2 = curNode2->next;
		}
		else {
			Node* newNode = (List*)malloc(sizeof(struct List));
			newNode->num = curNode1->num;
			newNode->next = NULL;

			curNode3->next = newNode;
			curNode3 = newNode;

			curNode1 = curNode1->next;
			curNode2 = curNode2->next;
		}
	}

最终代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct List {
	int num;
	struct List* next;
}Node;

int main() {
	List* list1 = (List*)malloc(sizeof(struct List));
	List* list2 = (List*)malloc(sizeof(struct List));
	List* list3 = (List*)malloc(sizeof(struct List));
	list1->next = NULL;
	list2->next = NULL;

    int n;

	Node* curNode1 = list1;
	scanf("%d", &n);
	while (n != -1) {
		Node* newNode = (List*)malloc(sizeof(struct List));
		newNode->num = n;
		newNode->next = NULL;

		curNode1->next = newNode;
		curNode1 = newNode;

		scanf("%d", &n);
	}

	Node* curNode2 = list2;
	scanf("%d", &n);
	while (n != -1) {
		Node* newNode = (List*)malloc(sizeof(struct List));
		newNode->num = n;
		newNode->next = NULL;

		curNode2->next = newNode;
		curNode2 = newNode;

		scanf("%d", &n);
	}

	curNode1 = list1->next;
	curNode2 = list2->next;
	Node* curNode3 = list3;

	while (curNode1 && curNode2) {
		if (curNode1->num < curNode2->num) {
			curNode1 = curNode1->next;
		}
		else if (curNode1->num > curNode2->num) {
			curNode2 = curNode2->next;
		}
		else {
			Node* newNode = (List*)malloc(sizeof(struct List));
			newNode->num = curNode1->num;
			newNode->next = NULL;

			curNode3->next = newNode;
			curNode3 = newNode;

			curNode1 = curNode1->next;
			curNode2 = curNode2->next;
		}
	}

	int flag = 1;
	curNode3 = list3->next;
	if (!curNode3) {
		printf("NULL");
		return 0;
	}
	else {
		while (curNode3) {
			if (flag) {
				printf("%d", curNode3->num);
				flag = 0;
			}
			else {
				printf(" %d", curNode3->num);
			}
			curNode3 = curNode3->next;
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值