[LeetCode] Intersection of Two Linked Lists 两链表是否相交
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
- If the two linked lists have no intersection at all, return
null
. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
- 遍历list1 ,将其节点存在hash_table
- 遍历list2,如果已经在hash_table中,那么存在
利用hash_table 可以提升时间到O(n+m),可是空间变O(n)了
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<ListNode*,int> m;
while(headA!=NULL){
m[headA] = ;
headA=headA->next;
}
while(headB!=NULL){
if(m[headB]==) return headB;
headB=headB->next;
}
return NULL;
}
};
- 判断list 是否有NULL 情况
- 同时遍历 两个新链表
- 如果节点地址相同,返回
- 如果不相同继续遍历
- 遍历结束返回NULL
#include <iostream>
#include <unordered_map>
using namespace std; /**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; /**
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<ListNode*,int> m;
while(headA!=NULL){
m[headA] = 1;
headA=headA->next;
}
while(headB!=NULL){
if(m[headB]==1) return headB;
headB=headB->next;
}
return NULL;
}
};
*/
class Solution{
public:
ListNode* getIntersectionNode(ListNode *headA,ListNode * headB)
{
ListNode * h1=headA;
ListNode * h2=headB;
if(headA==NULL||headB==NULL) return NULL;
bool flag1=true,flag2=true;
while(headA!=NULL&&headB!=NULL){
if(headA==headB) return headA;
headA=headA->next;
headB=headB->next;
if(headA==NULL&&flag1){ headA=h2; flag1 =false;}
if(headB==NULL&&flag2){ headB=h1; flag2 =false;}
}
return NULL;
}
}; int main()
{
ListNode head1();
ListNode head2();
ListNode node1();
ListNode node2();
head1.next = &node1;
node1.next = &node2;
head2.next = &node2;
Solution sol;
ListNode *ret = sol.getIntersectionNode(&head1,&head2);
if(ret==NULL) cout<<"NULL"<<endl;
else cout<<ret->val<<endl;
return ;
}
[LeetCode] Intersection of Two Linked Lists 两链表是否相交的更多相关文章
- Intersection of Two Linked Lists两链表找重合节点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- leetcode:Intersection of Two Linked Lists(两个链表的交叉点)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- intersection of two linked lists.(两个链表交叉的地方)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode——Intersection of Two Linked Lists
Description: Write a program to find the node at which the intersection of two singly linked lists b ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
随机推荐
- WebUploader 设置单个文件上传
1.导入控件样式文件 <link rel="stylesheet" type="text/css" href="__PUBLIC__/stati ...
- RGB555转RGB565
做tft彩屏显示图片的时候,显示16位位图,显示屏的显示模式为RGB565.使用img2lcd转换后的16位bmp,显示出来后,颜色有偏差:转换为565格式的bin文件,显示完全正常,可以确定转换为b ...
- 【bzoj4372】烁烁的游戏 动态点分治+线段树
题目描述 给一颗n个节点的树,边权均为1,初始点权均为0,m次操作:Q x:询问x的点权.M x d w:将树上与节点x距离不超过d的节点的点权均加上w. 输入 第一行两个正整数:n,m接下来的n-1 ...
- Contest 5
A:这我怎么没学傻了啊.整个一傻逼题一眼容斥我连暴力都写不出来啊.显然序列是没有什么用的,考虑求众数小于x的概率,显然可以枚举有几个超过容斥一发.虽然要算的组合数非常大,发现可以抵消很大一部分,最后算 ...
- 洛谷 P3177 树上染色
题面 题目要求将k个点染成黑色,求黑点两两距离及白点两两距离,使他们之和最大. 我们可以将距离转化为路径,然后再将路径路径拆分成边,就可以记录每条边被经过的次数,直接计算即可. 很简单对吧?那么问题来 ...
- P3916 图的遍历
题目描述 给出 NNN 个点, MMM 条边的有向图,对于每个点 vvv ,求 A(v)A(v)A(v) 表示从点 vvv 出发,能到达的编号最大的点. 输入输出格式 输入格式: 第1 行,2 个整数 ...
- windows7下的64位redis安装简介
在网上找了好多,指向的都是同一个地址,可惜打不开.https://github.com/MSOpenTech/redis/releases.网址被禁掉了.终于找到一篇有用的帖子,安装成功.感谢仁兄ko ...
- 解题:HNOI 2008 玩具装箱
题面 搞了一晚上斜率优化,大概懂了一点,写写 原来常用的优化dp方法:做前缀和,预处理,数据结构维护 现在有转移方程长这样的一类dp:$dp[i]=min(dp[i],k[i]*x[j]+y[j]+c ...
- poj1284 Primitive Roots
Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4775 Accepted: 2827 D ...
- windows 安装 apache 服务以及添加 php 解析
apache 官方并没有提供 windows 的安装包,但是它们官网给出了第三方的链接,我们可以在那些第三方网站上找到适用于 windows 的二进制包. 我们点进去下载一个 64 位的, 下载完之后 ...