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. ...
随机推荐
- 曹工说Redis源码(5)-- redis server 启动过程解析,以及EventLoop每次处理事件前的前置工作解析(下)
曹工说Redis源码(5)-- redis server 启动过程解析,eventLoop处理事件前的准备工作(下) 文章导航 Redis源码系列的初衷,是帮助我们更好地理解Redis,更懂Redis ...
- Jmeter使用Websocket插件测试SingalR,外加还有阿里云PTS的Jmeter原生测试爬坑日志。
题外话:距离我的上一篇博客已经过去7年多了,我实在是个不务正业的程序员,遇到测试方面的东西总想分享一下,因为可用的资料实在太少了(包括国外的资料). 本人不喜欢授人以鱼,所以不会直接给出问题和解决方案 ...
- 刨根问底系列(2)——stdin、stdout、FILE结构体、缓冲区和fflush的理解
stdin.stdout.FILE结构体.缓冲区和fflush理解 因为之前调试代码时, printf输出的字符串总是被截断了输出(先输出部分, 再输出剩余的), 当时调试了很久, 才知道问题所在, ...
- push和appendChild的区别
概述:绑定事件(push和appendChild用法相似:但是一个是控制数组,一个是控制元素节点)用法:1.数组1的更改后的长度 = 数组1.push();//用来控制数组,在数组最后面插入项,返回数 ...
- Mysql 错误 Connection is read-only 解决方式
环境Spring+Mybatis <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.s ...
- Reward 杭电 2647
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- 19.SpringCloud实战项目-SpringCloud整合Alibaba-Nacos配置中心
SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...
- 代理模式是什么?如何在 C# 中实现代理模式
代理模式 并不是日常开发工作中常常用到的一种设计模式,也是一种不易被理解的一种设计模式.但是它会广泛的应用在系统框架.业务框架中. 定义 它的 定义 就如其它同大部分 设计模式 的定义类似,即不通俗也 ...
- php +go关键字实现协程
来源: https://studygolang.com/articles/17631?fr=sidebar 今天在知乎浏览时忽然发现了一个有趣的东西,php竟然可以实现协程的实现,而且还是通过go关键 ...
- tp5--开发规范
在日常开发的过程中,写代码都要有一定的规范,不然可读取就太差了,所以为了以后的维护.对接,好的代码规定是必须的. 以下是我自己对自己提出的要求: 全部: 1) 每个方法都要写好备注(@retrun作 ...