求两个有序链表序列的交集 运行超时怎么办

周大侠 2014-07-24 10:18:34
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。

输入格式说明:

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

输出格式说明:

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

样例输入与输出:

序号 输入 输出
(1)
1 2 5 -1
2 4 5 8 10 -1
2 5
(2)
1 3 5 -1
2 4 6 8 10 -1
NULL
(3)
1 2 3 4 5 -1
1 2 3 4 5 -1
1 2 3 4 5
(4)
3 5 7 -1
2 3 4 5 6 7 8 -1
3 5 7
(5)
-1
10 100 1000 -1
NULL

我的代码是
#include <stdio.h>
#include <stdlib.h>

#define LEN sizeof(struct node)

typedef struct node{
int s;
struct node* next;
}Node;

Node* create()
{
Node *p1,*p2,*pHead;
pHead = NULL;
p1 = p2 = (Node *)malloc(LEN);
scanf("%d",&(p1->s));
while((p1->s)!=-1){
if(pHead == NULL)
pHead = p1;
else
p2->next = p1;
p2 = p1;
p1 = (Node *)malloc(LEN);
scanf("%d",&(p1->s));
}
p1->next = NULL;
if(pHead==NULL)
return p1;
else
return pHead;

}

//Do not need to save the intersection into a new pointer
void find_inter(Node *pHead1, Node *pHead2)
{
int a=0;
if(pHead1->next == NULL|pHead2->next == NULL){
printf("NULL");
return;
}
for(Node *p1 = pHead1; p1!= NULL; p1 = p1->next)
for(Node *p2 = pHead2; p2!= NULL; p2 = p2->next)
if(p1->s == p2->s)
{
if(a==0)
printf("%d",p1->s);
else
printf(" %d",p1->s);
++a; }
if(!a)
printf("NULL");

}

int main()
{
Node *p1,*p2;
p1=create();
p2=create();
find_inter(p1,p2);
}


提交上去后最后一个测试点说运行超时。的确,我的思路很简单,复杂度变成了O(N^2)
我知道用c++怎么能提交成功,但是希望可以用纯c的链表来处理,求指导
...全文
349 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
周大侠 2014-07-25
  • 打赏
  • 举报
回复
引用 1 楼 brookmill 的回复:
你这个思路完全忽视了“有序”这个重要的条件。 我这个O(n+m),我没测试,目测应该可以。
void find_inter(Node *pHead1, Node *pHead2)
{
  int found = 0;
  while (pHead1 != NULL && pHead2 != NULL) {
    if (pHead1->s < pHead2->s) {
      pHead1 = pHead1->next;
    } else if (pHead1->s > pHead2->s) {
      pHead2 = pHead2->next;
    } else {
      found = 1;
      printf("%d ", pHead1->s);
      pHead1 = pHead1->next;
      pHead2 = pHead2->next;
    }
  }
  if (!found) {
    printf("NULL");
  }
  printf("\n");
}
引用 1 楼 brookmill 的回复:
你这个思路完全忽视了“有序”这个重要的条件。 我这个O(n+m),我没测试,目测应该可以。
void find_inter(Node *pHead1, Node *pHead2)
{
  int found = 0;
  while (pHead1 != NULL && pHead2 != NULL) {
    if (pHead1->s < pHead2->s) {
      pHead1 = pHead1->next;
    } else if (pHead1->s > pHead2->s) {
      pHead2 = pHead2->next;
    } else {
      found = 1;
      printf("%d ", pHead1->s);
      pHead1 = pHead1->next;
      pHead2 = pHead2->next;
    }
  }
  if (!found) {
    printf("NULL");
  }
  printf("\n");
}
引用 1 楼 brookmill 的回复:
你这个思路完全忽视了“有序”这个重要的条件。 我这个O(n+m),我没测试,目测应该可以。
void find_inter(Node *pHead1, Node *pHead2)
{
  int found = 0;
  while (pHead1 != NULL && pHead2 != NULL) {
    if (pHead1->s < pHead2->s) {
      pHead1 = pHead1->next;
    } else if (pHead1->s > pHead2->s) {
      pHead2 = pHead2->next;
    } else {
      found = 1;
      printf("%d ", pHead1->s);
      pHead1 = pHead1->next;
      pHead2 = pHead2->next;
    }
  }
  if (!found) {
    printf("NULL");
  }
  printf("\n");
}
thx a lot,对对,想的太简单了,刚开始做算法题,开始以为是语言上面的问题
brookmill 2014-07-24
  • 打赏
  • 举报
回复
你这个思路完全忽视了“有序”这个重要的条件。 我这个O(n+m),我没测试,目测应该可以。
void find_inter(Node *pHead1, Node *pHead2)
{
  int found = 0;
  while (pHead1 != NULL && pHead2 != NULL) {
    if (pHead1->s < pHead2->s) {
      pHead1 = pHead1->next;
    } else if (pHead1->s > pHead2->s) {
      pHead2 = pHead2->next;
    } else {
      found = 1;
      printf("%d ", pHead1->s);
      pHead1 = pHead1->next;
      pHead2 = pHead2->next;
    }
  }
  if (!found) {
    printf("NULL");
  }
  printf("\n");
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧