C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3824 访问。
编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
A: a1 → a2
c1 → c2 → c3
B: b1 → b2 → b3
在节点 c1 开始相交。
注意:
如果两个链表没有交点,返回 null.
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
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.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3824 访问。
public class Program {
public static void Main(string[] args) {
var headA = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(3)
}
}
}
};
var headB = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(3) {
next = headA.next.next
}
}
};
var res = GetIntersectionNode(headA, headB);
ShowArray(res);
res = GetIntersectionNode2(headA, headB);
ShowArray(res);
res = GetIntersectionNode3(headA, headB);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(ListNode list) {
var node = list;
while(node != null) {
Console.Write($"{node.val} ");
node = node.next;
}
Console.WriteLine();
}
private static ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
//LeetCode超时未AC
var nodeA = headA;
while(nodeA != null) {
var nodeB = headB;
while(nodeB != null) {
if(nodeA == nodeB) {
return nodeA;
}
nodeB = nodeB.next;
}
nodeA = nodeA.next;
}
return null;
}
private static ListNode GetIntersectionNode2(ListNode headA, ListNode headB) {
var nodeA = headA;
var nodeB = headB;
var set = new HashSet<ListNode>();
while(nodeA != null) {
set.Add(nodeA);
nodeA = nodeA.next;
}
while(nodeB != null) {
if(set.Contains(nodeB)) {
return nodeB;
}
nodeB = nodeB.next;
}
return null;
}
private static ListNode GetIntersectionNode3(ListNode headA, ListNode headB) {
var pointA = headA;
var pointB = headB;
if(headA != null && headB != null) {
while(pointA != pointB) {
if(pointA == null) {
pointA = headB;
} else {
pointA = pointA.next;
}
if(pointB == null) {
pointB = headA;
} else {
pointB = pointB.next;
}
if((pointA == null) && (pointB == null)) {
return null;
}
}
return pointA;
} else {
return null;
}
}
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3824 访问。
1 2 3
1 2 3
1 2 3
分析:
显而易见,GetIntersectionNode 的时间复杂度为: ,GetIntersectionNode2 和GetIntersectionNode3 的时间复杂度为:
。
C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)的更多相关文章
- LeetCode 160: 相交链表 Intersection of Two Linked Lists
爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...
- [Swift]LeetCode160. 相交链表 | 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刷题总结-栈、链表、堆和队列篇
本文介绍LeetCode上有关栈.链表.堆和队列相关的算法题的考点,推荐刷题20道.具体考点分类如下图: 一.栈 1.数学问题 题号:85. 最大矩形,难度困难 题号:224. 基本计算器,难度困难 ...
- LeetCode刷题 --杂篇 --数组,链表,栈,队列
武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...
- C#LeetCode刷题之#707-设计链表(Design Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...
- C#LeetCode刷题之#234-回文链表(Palindrome Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3905 访问. 请判断一个链表是否为回文链表. 输入: 1-> ...
- C#LeetCode刷题之#141-环形链表(Linked List Cycle)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3901 访问. 给定一个链表,判断链表中是否有环. 进阶: 你能否 ...
- LeetCode刷题之合并排序链表
合并两个有序链表并返回一个新的列表.新列表应该由连接在一起的节点前两个列表 给定实例:Input: 1->2->4, 1->3->4Output: 1->1->2- ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
随机推荐
- Python Ethical Hacking - BACKDOORS(3)
BACKDOORS Sockets Problem: TCP is stream-based. Difficult to identify the end of message/batch. Solu ...
- vue : 对 vue-class-component 的个人理解
vue-class-component 是 vue 的官方库,作用是用类的方式编写组件. 这种编写方式可以让.vue文件的js域结构更扁平,并使vue组件可以使用继承.混入等高级特性. 简单的示例: ...
- 大型Java进阶专题(八)设计模式之适配器模式、装饰者模式和观察者模式
前言 今天开始我们专题的第八课了.本章节将介绍:三个设计模式,适配器模式.装饰者模式和观察者模式.通过学习适配器模式,可以优雅的解决代码功能的兼容问题.另外有重构需求的人群一定需要掌握装饰者模式. ...
- LGV定理
LGV定理用于解决路径不相交问题. 定理 有 \(n\) 个起点 \(1, 2, 3, ..., n\),它们 分别对应 要到 \(n\) 个终点 \(A, B, C, ..., X\),并且要求路径 ...
- ajax提交表单,包括跳入的坑!
本来今天上午写了一个js执行上下文的一个了解.但是写一大半的时候出去有事,电脑关了啥都没了. 还是让我们进入正题 ajax提交表单,很简单,原生js的代码太复杂,我们就jq的去写. 创建html文件, ...
- 基于jqgrid + ashx + nhibernate的分页
因为我目前运维的是一个webform项目,项目中未用到分页的功能,我百度了很多文章也没有一篇是结合jqgrid + ashx + nhibernate的分页,可能是因为后台要请求ashx的原因,不像m ...
- 好用到飞起的12个jupyter lab插件
1 简介 jupyter lab作为jupyter notebook的升级改造版,除了更加人性化的交互界面以及更多的用户自主定制功能之外,最吸引人的就是其丰富多样的拓展插件,使得每个使用jupyter ...
- 12天,这本《重学Java设计模式》PDF书籍下载量9k,新增粉丝1400人,Github上全球推荐榜!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言
- 浅谈Redis未授权访问漏洞
Redis未授权访问漏洞 Redis是一种key-value键值对的非关系型数据库 默认情况下绑定在127.0.0.1:6379,在没有进行采用相关的策略,如添加防火墙规则避免其他非信任来源ip访问等 ...
- Qt自定义控件之仪表盘1--简单的贴图仪表盘
0.前言 学程序首先要输出hell world,学电子要先来个流水灯.学Qt,那就必须先来个自定义控件,若有人问我从哪个下手,我推荐仪表盘,可简可繁,从低配到高配齐全,可入门也可进阶. 1.仪表盘解析 ...