两个有序链表序列的交集
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。
输入样例:
1 2 5 -1
2 4 5 8 10 -1
输出样例:
2 5
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
struct Node {
int data;
Node* next;
};
bool Locate(Node* l, int v) {
Node* p = l;
while (p) {
if (p->data == v)
return true;
p = p->next;
}
return false;
}
void print(Node* head) {
if (head == NULL)
printf("NULL");
bool flag = false;
Node* p = head;
while (p) {
printf("%s%d", flag ? " " : "", p->data);
flag = true;
p = p->next;
}
}
void merge(Node* La, Node* Lb, Node* &Lc) { //Lc 指针要使用引用
Node* q, * p, * head, * rear;
q = La;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
rear = head;
while (q) {
int a = q->data;
if (Locate(Lb, a)) {
p = (Node*)malloc(sizeof(Node));
p->data = a;
p->next = NULL;
rear->next = p;
rear = p;
}
q = q->next;
}
Lc = head->next;
}
int main() {
Node* LA, * LB, * LC;
LA = (Node*)malloc(sizeof(Node));
LB = (Node*)malloc(sizeof(Node));
LC = NULL;
int d = 0;
Node* head, * rear, * p;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
rear = head;
for(cin>>d;~d;cin>>d){
p = (Node*)malloc(sizeof(Node));
p->data = d;
p->next = rear->next;
rear->next = p;
rear = p;
}
LA = head->next;
head->next = NULL;
rear = head;
d = 0;
for (scanf("%d", &d);~d; scanf("%d", &d)) {
p = (Node*)malloc(sizeof(Node));
p->data = d;
p->next = rear->next;
rear->next = p;
rear = p;
}
LB = head->next;
merge(LA, LB, LC);
print(LC);
return 0;
}
//这个是错误代码,运行时大数据样例会超时
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
int data;
struct ListNode* next;
};
typedef struct ListNode node;
int main()
{
int i;
node l1,l2,l3;
l1.next=NULL;
l2.next = NULL;
l3.next = NULL;
node* p = &l1;
char c;
while((scanf("%d",&i))&&i!=-1)
{
node* s = (node*)malloc(sizeof(node));
s->data = i;
s->next = NULL;
p->next=s;
p=p->next;
}
p=&l2;
while((scanf("%d",&i))&&i!=-1)
{
node* s = (node*)malloc(sizeof(node));
s->data = i;
s->next = NULL;
p->next=s;
p=p->next;
}
p=l1.next;
node *q=l2.next;
node *m = &l3;
while((p!=NULL)&&(q!=NULL))
{
if((p->data)<(q->data))
{
p=p->next;
}
else if((p->data)>(q->data))
{
q=q->next;
}
else if((p->data)==(q->data))
{
node* s = (node*)malloc(sizeof(node));
s->data = p->data;
s->next = NULL;
m->next = s;
m= m->next;
p = p->next;
q = q->next;
}
}
m=l3.next;
if(l3.next==NULL)
{ printf("NULL");return 0;}
while(m!=NULL)
{
if(m->next==NULL)
printf("%d",m->data);
else
printf("%d ",m->data);
m=m->next;
}
return 0;
}
//别人的正确代码
————————————————
版权声明:本文为CSDN博主「Vincents Blog」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ww1473345713/article/details/51926144
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a, b, ans;
inline const int read()
{
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
return x * f;
}
int main()
{
int n;
while (~(n = read())) a.push_back(n);
while (~(n = read())) b.push_back(n);
set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(ans)); //使用set_intersection函数,求两个容器的交集
//相同还有set_union函数等
bool flag = false;
for (auto it : ans)
{
printf("%s%d", flag ? " " : "", it);
flag = true;
}
if (ans.empty()) printf("NULL");
return 0;
}
//简单版