进阶实验2-3.3 两个有序链表序列的交集 (20分)

PTA习题:进阶实验2-3.3 两个有序链表序列的交集 (20分)
已知两个非降序链表序列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>
struct Node
{
 int data;
 struct Node *Next;
};
struct Node *Read();
struct Node * func(struct Node *L1, struct Node *L2); //找交集函数声明
void print(struct Node *L);
int main()
{
 struct Node *L1, *L2, *L;
 L1 = Read();
 L2 = Read();
 L = func(L1, L2);
 print(L);
 return 0;
}
struct Node *Read()
{
 struct Node *head, *p, *tail;
 int num;
 head = NULL;
 tail = head;
 scanf("%d", &num);
 while (num != -1)
 {
  p = (struct Node *)malloc(sizeof(struct Node));
  p->data = num;
  p->Next = NULL;
  if (head == NULL) { head = p; tail = p; }
  else { tail->Next = p; tail = p; }
  scanf("%d", &num);
 }
 return head;
}
struct Node * func(struct Node *L1, struct Node *L2)
{
 struct Node *head, *p, *q, *m, *tail;
 head = NULL; tail = head;
 p = L1;
 q = L2;
 if (L1 != NULL && L2 != NULL)
 {
  while (p != NULL && q != NULL)
  {
   if (p->data > q->data)
   {
    q = q->Next;
   }
   else if (p->data < q->data)
   {
    p = p->Next;
   }
   else if (p->data == q->data)
   {
    m = (struct Node *)malloc(sizeof(struct Node));
    m->data = p->data;
    m->Next = NULL;
    if (head == NULL)
    {
     head = m;
     tail = m;
    }
    else
    {
     tail->Next = m;
     tail = m;
    }
    p = p->Next;
    q = q->Next;
   }
   }
 }
 return head;
}
void print(struct Node *L)
{
 struct Node *p;
 if (L == NULL) { printf("NULL"); }
 else {
  for (p = L; (p->Next) != NULL; p = p->Next)
  {
   printf("%d ", p->data);
  }
  printf("%d", p->data);
 }
}

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

#include<stdio.h>
#include<stdlib.h>
struct Node
{
 int data;
 struct Node *Next;
};
struct Node *Read();
struct Node * func(struct Node *L1, struct Node *L2);
void print(struct Node *L);
int main()
{
 struct Node *L1, *L2, *L;
 L1 = Read();
 L2 = Read();
 L = func(L1, L2);
 print(L);
 return 0;
}
struct Node *Read()
{
 struct Node *head, *p, *tail;
 int num;
 head = NULL;
 tail = head;
 scanf("%d", &num);
 while (num != -1)
 {
  p = (struct Node *)malloc(sizeof(struct Node));
  p->data = num;
  p->Next = NULL;
  if (head == NULL) { head = p; tail = p; }
  else { tail->Next = p; tail = p; }
  scanf("%d", &num);
 }
 return head;
}
struct Node * func(struct Node *L1, struct Node *L2)
{
 struct Node *head, *p, *q, *m, *tail;
 head = NULL; tail = head;
 if (L1 != NULL && L2 != NULL)
 {
  for (p = L1; p != NULL; p = p->Next)
  {
   for (q = L2; q != NULL; q = q->Next)
   {
    if (p->data == q->data)
    {
     m = (struct Node *)malloc(sizeof(struct Node));
     m->data = p->data;
     m->Next = NULL;
     if (head == NULL)
     {
      head = m;
      tail = m;
     }
     else
     {
      tail->Next = m;
      tail = m;
     }
    }
   }
  }
 }
 return head;
}
void print(struct Node *L)
{
 struct Node *p;
 if (L == NULL) { printf("NULL"); }
 else {
  for (p = L; (p->Next) != NULL; p = p->Next)
  {
   printf("%d ", p->data);
  }
  printf("%d", p->data);
 }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 题目描述: 给定两个递增排序的链表,本题要求将两个链表中的公共部输出。 输入格式: 输入2行,别在每行给出由若干个正整数构成的递增链表,每个正整数用空格开。数字和链表的长度N (1≤N≤10^5),数字大小不超过10^6。 输出格式: 在一行中输出两个输入链表的公共部,数字间以空格开,但行末不能有多余空格。 输入样例: 3 4 5 6 2 4 6 8 10 输出样例: 4 6 解题思路: 本题需要输出两个有序链表交集,可以使用双指针的方法,别指向两个链表的头结点,然后比较两个指针所指向的节点的值,如果相等,则输出该值,并将两个指针都向后移动一位,否则将值较小的指针向后移动一位,直到其中一个链表遍历完为止。 代码实现: ### 回答2: 这道题要求我们找到两个有序链表交集。由于链表已经是有序的,我们可以采用双指针的方法来遍历两个链表。 我们定义两个指针,p1 和 p2,别指向两个链表的头结点。然后,我们依次比较两个指针指向的节点的值,如果相等,说明这个节点是两个链表交集元素,我们将它保存下来,并让两个指针都往后移动一位;如果p1指向的节点的值小于p2指向的节点的值,那么p1往后移动一位,否则p2往后移动一位。不断重复这个过程,直到其中一个链表被遍历完为止。 下面是具体的代码实现: ``` class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def getIntersectionNode(headA: ListNode, headB: ListNode) -> ListNode: p1, p2 = headA, headB ans = [] while p1 and p2: if p1.val == p2.val: ans.append(p1.val) p1 = p1.next p2 = p2.next elif p1.val < p2.val: p1 = p1.next else: p2 = p2.next return ans ``` 时间复杂度:O(m+n) 空间复杂度:O(min(m,n)) 可以看出,该算法时间复杂度为O(m+n),其中m和n别是两个链表的长度。因为我们从头到尾遍历了两个链表,并且每个节点最多被访问一次,所以时间复杂度为O(m+n)。空间复杂度为O(min(m,n)),因为我们只需要保存交集元素。因此,我们可以得出,这个算法是比较优秀的。 ### 回答3: 题目描述: 给定两个递增序列 A 和 B ,请编写算法输出它们的交集序列。 输入格式: 第一行输入序列 A 的长度 N(0≤N≤10^5);第二行输入由 N 个非负整数成的递增序列 A 的元素; 第三行输入序列 B 的长度 M(0≤M≤10^5);第四行输入由 M 个非负整数成的递增序列 B 的元素; 输出格式: 输出由两个序列交集成的非递减序列,数字之间用一个空格隔开。若这两个序列交集为空,则输出空行。注意,需要去除重复元素。 解题思路: 将两个都存放到 map 中,key 为数值,value 为出现次数。遍历两个 map,若有相同 key,取 value 小的放入结果数中,并将两个 map 对应 key 的 value 减小相应的值。最终将结果数按从小到大排序输出。 代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值