已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL
。
输入样例:
1 2 5 -1
2 4 5 8 10 -1
结尾无空行
输出样例:
2 5
#include <iostream>
#include<algorithm>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}node,*ptr;
ptr creat(node* &l)
{
int n;
cin >> n;
/*
//头部插入法
while (n!=-1)
{
node* p = (node*)malloc(sizeof(node));
p->data=n;
//将新节点插入到头节点后面
p->next = l->next;
l->next = p;
cin >> n;
}
return l;
*/
//尾部插入法
node* r = l;
while (n!=-1)
{
node* p = (node*)malloc(sizeof(node));
p->data = n;
r->next = p;
r =