[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 开始交 ...
随机推荐
- dat.gui 上手
dat.gui是款神器产品.一个调试利器.但是用起来很简单很简单 1:引用dat.gui.js. 2:实例化 this.gui = new dat.GUI(); 3:创建可设置一个数据对象.例如v ...
- webgl example1
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 20个PHP程序性能优化的方法
1.数据库连接当使用完毕时应当关闭,不要使用长连接. 2.用@屏蔽错误消息的做法非常低效,极其低效. 3.使用选择分支语句(switch case)好于使用多个if,else if. 4.$row[' ...
- HDU4472_Count
题目的意思简单,给你n个点,每个点(除根节点)都有且只有一个父节点.现在问你这些节点关系组成的图有多少种情况满足同一层节点上每个点的后继节点数量相等. 很简单,对于n节点,直接枚举n-1的的所有约数情 ...
- pyhcarm github
1.主题 介绍如何用Pycharm实打实的创建.运行.调试程序. 2.准备工作 Pycharm版本为2.7或者更高. 至少安装一个Python解释器,2.4到3.3均可 3.下载安装Pycharm 下 ...
- WebGL画一个10px大小的点
WebGL程序在屏幕上同时使用HTML和javascript来创建和显示三维图形.WebGL中新引入的<canvas>元素标签,它定义了网页上的绘图区域. 由于<canvas> ...
- BZOJ4890 Tjoi2017城市
显然删掉的边肯定是直径上的边.考虑枚举删哪一条.然后考虑怎么连.显然新边应该满足其两端点在各自树中作为根能使树深度最小.只要线性求出这个东西就可以了,这与求树的重心的过程类似. #include< ...
- pthread的pthread_join()函数理解实验
一.使用方式 pthread_t tid;pthread_create(&tid, NULL, thread_run,NULL);pthread_join(tid,NULL);创建线程之后直接 ...
- Java之面向对象编程20170619
/*************************************************************************************************** ...
- 电商网站中价格的精确计算(使用BigDecimal进行精确运算(实现加减乘除运算))
使用BigDecimal的String的构造器.商业计算中,使用bigdecimal的String构造器,一定要用. 重要的事情说三遍: 商业计算中,使用bigdecimal的String构造器! 商 ...