c++ 取两个链表的交集

Problem statement: Write a C++ program to find the intersection of two single linked lists.

问题陈述:编写一个C ++程序来查找两个单个链表的交集。

Example:

例:

    Let the first linked list be:
6->5->2->9->NULL
Let the second linked list to be:
2->7->NULL
So there intersection is:
2->NULL

Solution

Brute force approach:

蛮力法:

One technique can be to traverse all the nodes of a linked list and check whether the traversed node is in the other linked list or not. Such an approach takes O(m*n) times complexity. m, n=length of linked lists.

一种技术可以是遍历链表的所有节点,并检查遍历的节点是否在另一链表中。 这种方法需要O(m * n)乘以复杂度mn =链表的长度

Efficient approach:

高效的方法:

Efficient approach use to use merge sort.

高效的方法用于使用合并排序

  1. Sort both the linked list using merge sort. ( for detailed refer to: Merge sort for single linked lists)

    使用合并排序对两个链表进行排序。 (有关详细信息,请参阅: 合并单个链接列表的排序 )

  2. Scan each linked list and build intersection according to following:

    扫描每个链表并按照以下步骤建立交集:

  3. IF (list1->data < list2->data)
    No intersection found
    Traverse list1 by one step( list1=list1->next)
    ELSE IF(list1->data ==list2->data)
    Createnode(list1->data) && append node to the intersection list
    Traverse list1 by one step( list1=list1->next)
    Traverse list2 by one step( list2=list2->next)
    ELSE//list1->data >list2->data
    No intersection found
    Traverse list2 by one step( list2=list2->next)
    END IF-ELSE
    
    
  4. Display the intersection list

    显示路口清单

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C ++实现查找两个链表的交集 (C++ implementation to find intersection of two linked lists)

#include<bits/stdc++.h>
using namespace std;
class node{public:
int data; // data field
struct node *next;
};
void display(class node* head){node* current=head; // current node set to head
//traverse until current node isn't NULL
while(current!=NULL){
printf("%d ",current->data);
current=current->next; // go to next node
}
}
node* creatnode(int d){node* temp=(node*)malloc(sizeof(node));
temp->data=d;
temp->next=NULL;
return temp;
}
//merging two sorted list
node* mergeList(node* split1,node* split2){node* newhead=NULL;
//base cases
if(split1==NULL)
return split2;
if(split2==NULL)
return split1;
if(split1->data<=split2->data){newhead=split1;
newhead->next=mergeList(split1->next,split2);
}
else{newhead=split2;
newhead->next=mergeList(split1,split2->next);
}
return newhead;
}
//split list for merge sort
void splitList(node* head,node** split1,node** split2){
node* slow=head;
node* fast=head->next;
while(fast!=NULL){fast=fast->next;
if(fast!=NULL){slow=slow->next;
fast=fast->next;
}
}
*split1=head;
*split2=slow->next;
//spliting
slow->next=NULL;
}
//merge sort
void mergeSort(node** refToHead){
node* head=*refToHead;
node* split1,*split2;
//base case
if(head==NULL || head->next==NULL){return;
}
//split the list in two halves
splitList(head,&split1,&split2);
//recursively sort the two halves
mergeSort(&split1);
mergeSort(&split2);
*refToHead=mergeList(split1,split2);
return;
}
node* findIntersection(node* head1, node* head2){//base case
if(head1==NULL && head2==NULL)
return NULL;
node* head4=NULL,*temp;
//for inserting the first common node
while( head1!=NULL && head2!=NULL && head4==NULL){
if(head1->data<head2->data){head1=head1->next;
}
//intersection nodes(intersection)
else if(head1->data==head2->data){
head4=creatnode(head1->data);
temp=head4;
head1=head1->next;
head2=head2->next;
}
else{head2=head2->next;
}
}
//for other common nodes(intersection)
while( head1!=NULL && head2!=NULL){
if(head1->data<head2->data){head1=head1->next;
}
//intersection nodes
else if(head1->data==head2->data){
temp->next=creatnode(head1->data);
temp=temp->next;
head1=head1->next;
head2=head2->next;
}
else{head2=head2->next;
}
}
return head4;
}
int main(){printf("creating the linked list by inserting new nodes at the end\n");
printf("enter 0 to stop building the list, else enter any integer\n");
int k;
node* curr,*temp;
cout<<"enter list1...\n";
scanf("%d",&k);
node* head1=creatnode(k); //buliding list, first node
scanf("%d",&k);
temp=head1;
///inserting at the end//
while(k){curr=creatnode(k);
temp->next=curr;//appending each node
temp=temp->next;
scanf("%d",&k);
}
cout<<"displaying list1...\n";
display(head1); // displaying the list
cout<<"\nenter list2...\n";
scanf("%d",&k);
node* head2=creatnode(k); //buliding list, first node
scanf("%d",&k);
temp=head2;
///inserting at the end//
while(k){curr=creatnode(k);
temp->next=curr;//appending each node
temp=temp->next;
scanf("%d",&k);
}
cout<<"displaying list1...\n";
display(head2);
//sort both the lists
mergeSort(&head1);
mergeSort(&head2);
cout<<"\ndisplaying their intersection...\n";
node* head4=findIntersection(head1,head2);
if(head4)
display(head4);
else
cout<<"No intersection found\n";
return 0;
}

Output

输出量

First run:
creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
enter list1...
6 5 2 9 0
displaying list1...
6 5 2 9
enter list2...
2 7 0
displaying list1...
2 7
displaying their intersection...
2
Second run:
creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
enter list1...
6 7 8 0
displaying list1...
6 7 8
enter list2...
1 5 2 0
displaying list1...
1 5 2
displaying their intersection...
No intersection found
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Example with explanation:

带有说明的示例:

First linked list: 6->5->2->9->NULL
Second linked list: 2->7->NULL
After applying merge sort:
First linked list: 2->5->6->9->NULL
Second linked list: 2->7->NULL
------------------------------------------------------
//for better understanding nodes are represented
//only by respective values
head1=2
head2=2
FindIntersection(2,2):
0th iteration
head1!=NULL && head2!=NULL
head1->data==head2->data //=2
thus head4(head of intersection list) =2
temp=head4=2
intersection list up to now:
2->NULL
head1=2->next=5;
head2=2->next=7;
1st iteration
head1!=NULL && head2!=NULL
head1->data<head2->data //5<7
thushead1=5->next=6;
temp=same as before
intersection list up to now:
2->NULL
head2=same as before;
2nd iteration
head1!=NULL && head2!=NULL
thushead1=6->next=9;
temp=same as before
intersection list up to now:
2->NULL
head2=same as before;
3rd iteration
head1!=NULL && head2!=NULL
head1->data>head2->data //9>7
thushead2=7->next=NULL;
temp=same as before
intersection list up to now:
2->NULL
Head1=same as before;
4th iteration
Head2=NULL
So, iteration stops & intersection list is build:
2->NULL

翻译自: https://www.includehelp.com/cpp-programs/find-intersection-of-two-linked-lists.aspx

c++ 取两个链表的交集

c++ 取两个链表的交集_使用C ++程序查找两个链表的交集相关推荐

  1. c ++ 链表_C ++程序查找两个单个链表的并集

    c ++ 链表 Problem statement: Write a C++ program to find the union of two single linked lists. 问题陈述:编写 ...

  2. 【iOS高级资深工程师面试篇】⑪、2022年,金九银十我为你准备了《iOS高级资深工程师面试知识总结》 算法部分 字符串反转-链表反转-有序数组组合-Hash算法-查找两个子视图的共同父视图

    iOS高级资深工程师面试篇系列 - 已更新3篇 UI部分1/3 -UITableView-事件传递&视图响应 UI部分2/3 -图像显示原理-UI卡顿&掉帧 UI部分3/3 -UIVi ...

  3. 运行python程序的两种方式交互式和文件式_执行Python程序的两种方式

    交互式(了解) 交互式环境下,敲完一条命令按下enter键马上能看到结果,调试程序方便.程序无法永久保存,关掉cmd窗口数据就消失了. 命令行式(了解) 打开文本编辑器,在文本编辑器中写入一串字符. ...

  4. 用c 语言实现数组的并集,C ++程序查找两个未排序数组的并集和交集

    在本文中,我们将讨论一个程序来查找两个给定的未排序数组的并集和交集. 让我们用" A"和" B"表示两个数组.然后这些数组的并集用A∪B表示,它基本上是两个给定 ...

  5. python有两个运行程序分别是什么_运行python程序的两种方式

    [单选题]I wonder why ________ are so interested in action movies. [单选题]项目经理的职责不包括以下哪项内容 ? [简答题]结合项目的特点和 ...

  6. python打开方式包括_运行Python程序有两种方式:________和________。

    运行Python程序有两种方式:________和________. 答: 交互式 文件式 自觉的人,心目中有一张蓝图,人生理想.但是不知道世界的游戏规则 答:× 山水画的鉴赏方法? 答:1.首先看风 ...

  7. excel两个表格数据对比_快速对比excel表格两个sheet中不同的数据,极大的降低重复工作量...

    两个Excel表格应该比较,公式不会被设置,而vba是一本天书.有简单的方法吗? 有!有!有!!! Excel有一个不是所有人都常用的功能:合并计算.使用它,我们可以快速比较两个表之间的差异. 示例: ...

  8. c++ std 方法 取两个数的较大_【数据结构C++】两数交换(4种方法)

    一.语言:C++ 二.内容:通过函数调用实现两个数的交换 三.方法: 正常函数交换 引用类型的函数交换 指针类型的函数交换 宏函数定义交换(不常见) #include <iostream> ...

  9. JAVA输出两个顺序链表的并集_(顺序表)两个集合求并集-洋葱先生-杨少通

    注:本程序由Visual Studio 2015编写,与VC++6.0稍有区别,复制到VC++6.0下注释掉"#include "stdafx.h""即可运行, ...

最新文章

  1. java rhino import_java调用javascript :js引擎rhino
  2. Eclipse中安装插件时提示:No repository found containing: osgi.bundle,org.eclipse.emf,2.8.0.v20180706-1146
  3. 【音视频技术与全球化】
  4. 12.学习Camera之——android binder 机制架构
  5. 建站之星安装提示无法连接数据库
  6. 苹果手机壳_手机壳推荐:iPhone12手机壳苹果保护壳平价个性推荐
  7. Edge地址栏搜索引擎换成Bing
  8. 知识图谱下图神经网络、图计算、图数据、图数据库未来发展趋势如何?
  9. 分布式之CAP原则详解
  10. Excel技巧—一个公式实现中英文翻译
  11. 哈勃(Hubble)太空望远镜:人类的大眼睛
  12. 桌面html文件图标异常,.htm.html文件图标无法显示的解决办法
  13. ipad一直卡在白苹果_iPad 为什么至今都没有天气和计算器应用?苹果回答来了
  14. nodejs require/import导包报错以及解决方法,chalk-animation库使用
  15. Python3.5 Django1.10 Scrapy1.2 Ubuntu16.04 HTML5
  16. 树链剖分——树链剖分模板
  17. 省级面板数据(2000-2019)七:价格指数(居民消费、零售、生产、投资)(stata版)
  18. CSAPP 拆炸弹 中科大实验
  19. lightroom使用小结五 调整作品曝光
  20. 【Python茴香豆系列】之 PANDAS 如何遍历 DataFrame 的所有行

热门文章

  1. python中、文件最重要的功能是( )和接收数据_Python基础语法14个知识点大串讲
  2. js css模仿打字效果
  3. 移动端 fixed 固定按钮在屏幕下方,然后按钮被键盘顶上来...顶上来了有没有~
  4. css3 flex 布局
  5. javascript 数组求交集/差集/并集/过滤重复
  6. Python 基础知识整理-2
  7. C语言结构体及函数传递数组參数演示样例
  8. Eclipse安装以及JDK环境变量配置
  9. java连接oracle数据库 -- jdbc连接
  10. zlib1.2.5的编译