Leetcode45: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.
/**
* 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) {
<span style="white-space:pre"> </span>int lenA = 0;
int lenB = 0;
ListNode *head1 = headA;
<span style="white-space:pre"> </span>ListNode *head2 = headB;
while (head1!=NULL)
{
head1 = head1->next;
lenA++;
}
while (head2!=NULL)
{
head2 = head2->next;
lenB++;
}
int n;
if (lenA < lenB)
{
n = lenB - lenA;
head1 = headA;
head2 = headB;
for (int i = 0; i < n; i++)
{
head2 = head2->next;
}
}
else
{
n = lenA - lenB;
head1 = headA;
head2 = headB;
for (int i = 0; i < n; i++)
{
head1 = head1->next;
}
} while (head1 != head2)
{
head1 = head1->next;
head2 = head2->next;
} return head1;
}
};
Leetcode45:Intersection of Two Linked Lists的更多相关文章
- 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 whic ...
- 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 ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- [LeetCode 题解]:Intersection of Two Linked Lists
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- [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] 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: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
随机推荐
- 浅谈自学Python之路(购物车程序练习)
购物车程序练习 今天我们来做一个购物车的程序联系,首先要理清思路 购物车程序需要用到什么知识点 需要用到哪些循环 程序编写过程中考虑值的类型,是int型还是字符串 如果值为字符串该怎么转成int型 用 ...
- [Swift]LeetCode1064. 不动点 | Fixed Point
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Java调用JavaWebService
1.pom配置 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...
- android 提纲挈领
之后的android学习将侧重三方面: 1.基础内容例如xml属性.sharedpreference.数据库必须能够熟记于心. 2.开源library熟练应用,能够了解如何更好地使用各种开源libra ...
- 【java基础】(3)Java继承内存分配
继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子类中定义的成员变量和父类中定义 ...
- 使用 CSS 追踪用户
原文地址:Crooked Style Sheets 作者:jbtronics 除了使用 JS 追踪用户,现在有人提出了还可以使用 CSS 进行网页追踪和分析,译者认为,这种方式更为 优雅,更为 简洁, ...
- Memcached 之缓存雪崩现象、实际案例和缓存无底洞现象
一.缓存雪崩现象 由于集群中某个memcached服务器宕机的原因,造成集群中的服务器命中率下降.只能通过访问数据库得到数据,是的数据库的压力倍增,造成数据库服务器崩溃.重启数据库还是会崩溃,但是数据 ...
- Linux循环
echo 'for循环' do echo $i done
- GreenPlum 集群常用命令
GreenPlum 常用命令 gpstate 命令 参数 作用 gpstate -b => 显示简要状态 gpstate -c => 显示主镜像映射 gpstart -d => 指定 ...
- 2.Eclipse中绑定Tomcat
1.下载并成功安装Eclipse和Tomcat(该步骤有问题请查看我的相关经验): 2.打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 3.找到Server下方的 ...