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.
solution:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
int lenA = ;
int lenB = ;
ListNode *pA = headA;
while (pA != NULL)
{
++lenA;
pA = pA->next;
}
ListNode *pB = headB;
while (pB != NULL)
{
++lenB;
pB = pB->next;
} if (pA != pB)
return NULL; pA = headA;
pB = headB; if (lenA > lenB)
{
int k = lenA - lenB;
while (k--)
{
pA = pA->next;
}
}
else
{
int k = lenB - lenA;
while (k--)
{
pB = pB->next;
}
} while (pA != pB)
{
pA = pA->next;
pB = pB->next;
}
return pA;
}
上述解法来自《剑指offer》,还有一种基于Hashset的方法。
solution:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
unordered_set<ListNode*> st;
while (headA != NULL)
{
st.insert(headA);
headA = headA->next;
} while (headB != NULL)
{
if (st.find(headB) != st.end())
return headB;
headB = headB->next;
}
return NULL;
}
Intersection of Two Linked Lists (求两个单链表的相交结点)的更多相关文章
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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] 160. 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 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode OJ: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 160. 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(两个链表的第一个公共节点)
来源:https://leetcode.com/problems/intersection-of-two-linked-lists Write a program to find the node a ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
随机推荐
- 从零搭建一个SpringCloud项目之Config(五)
配置中心 一.配置中心服务端 新建项目study-config-server 引入依赖 <dependency> <groupId>org.springframework.cl ...
- jetCache 基本使用
1.pom引用 <!--jetcache缓存 lettuce--> <dependency> <groupId>com.alicp.jetcache</gro ...
- shell命令(一)
什么是shell? Shell是一个应用程序,它连接了用户和Linux内核,让用户能够更加高效.安全.低成本地使用Linux内核,这就是Shell的本质. shell与Linux系统关系图 shell ...
- AJ学IOS 之微博项目实战(1)微博主框架-子控制器的添加
AJ分享,必须精品 一:简单介绍 这是新浪微博的iOS端项目,来自于黑马的一个实战项目. 主要分成五大模块,本次全部运用纯代码实现,其中会用到很多前面学过得内容,如果有的地方有重复的知识点,说明这个知 ...
- A. Number Theory Problem
题目大意:计算小于2^n,且满足2^k-1并且是7的倍数的个数 思路:优先打表,数据不大,1e5,然后求个前n项和 #include<bits/stdc++.h> using namesp ...
- lua使用笔记2:Linux 中安装php的lua扩展
安装lua扩展的前提是lua已经安装好,如果没有安装,参照 1.http://pecl.php.net/package/lua 下载lua扩展 或者Linux下直接输入 wget http://pec ...
- Unity 游戏框架搭建 2019 (三十、三十一) MenuItem 显示顺序问题 & 类的提取
在上一篇,我们得出了两个核心的学习思路: 根据问题去学习,并收集. 主动学习,并思考适用场景. 我们今天解决 MenuItem 显示顺序问题. 目前 MenuItem 显示如图所示: 我们来看下 Me ...
- 常见web漏洞的整理之SQL注入
SQL注入: 简介: 全称Structured Query Language,即结构化查询语言,是一种特殊的编程语言,用于数据库中的标准数据查询语言.也被作为关系式数据库管理系统的标准语言. 原理: ...
- Springboot:logback日志管理(九)
Springboot默认使用的日志框架就是logback 创建自定义的logback-spring.xml放在resources类目录下即可 logback-spring.xml: <?xml ...
- 开发一款图片压缩工具(二):使用 pngquant 实现图片压缩
上一篇我尝试使用了 pillow 库对 png 图片进行了压缩,效果不好.这次我换用 pngquant 来压缩.pngquant 是用于 PNG 图像有损压缩的命令行实用程序和库.压缩程序会显著减小文 ...