[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 开始交 ...
随机推荐
- 将Python项目生成所有依赖包的清单requirements .txt文件
在开发中不同的项目总会牵扯到各种不同作用的包安装,下面是总结一下对写好的项目自动生成依赖清单,以及在新环境下解决依赖的方法: 一:生成所有依赖清单requirements.txt 这里需要使用到的工具 ...
- 在Mac上安装mysql数据库
安装 登录MySQL网站 用dmg的方式安装.Download MySQL Community Server 或者常规方式,打开官网 : http://www.mysql.com/downloads/ ...
- C++之基础知识20170830
/*************************************************************************************************** ...
- 对于redis底层框架的理解(五)
之前总结了redis的通讯流程,基本框架,epoll的封装等等,这次介绍下 redis对于select模型的封装 //select 模型 typedef struct aeApiState { //读 ...
- python基础5--模块
模块 一.模块简介 模块是一个包含有定义的函数和变量的文件,其后缀名是.py.Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持. 标准 ...
- JavaSE的学习路线
基于现阶段的JavaEE学习的对象,主要是趋向于Web的方向,主要就是说在JavaWeb的基础上进行进一步的开发和学习,下面我会将自己总结的对于自己的一点关于JavaEE学习路线会逐步讲解. 第一部分 ...
- poi对EXCEL的操作(一)
(原创自己这段时间对poi的研究心得) 一.基础的对象 1.wookbook工作簿 创建工作簿 wookbook XSSFWorkbook类的构造方法 XSSFWorkbook ...
- struts2的action中@Autowired注入为null的解决方案
今天遇到类似问题,记录下来以便以后查阅: @Aspect作用于action,致使action中的@Autowired注入为null的解决方案,以下三种任选一种: 1.去掉@Autowired,改用se ...
- spring boot(一):入门
Spring Boot的优点 Spring Boot 是伴随着 Spring 4.0 诞生的,从字面理解,Boot是引导的意思,因此 Spring Boot 旨在帮助开发者快速搭建 Spring 框架 ...
- 使用纯 CSS 实现响应式的图片显示效果
有许多方法可以实现页面里图像的响应式显示(Responsive).然而,我碰到的所有方案都使用了JavaScript.这使我疑惑不用 JavaScript 实现图像响应是否可行. 我提出了下面纯CSS ...