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 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
随机推荐
- Go的100天之旅-06数组和Slice
目录 数组 Slice 数组 Go的数组和其它语言基本上一样,是长度固定的特定类型元素组成的序列,这基本上是所有语言数组的特性.和其它语言相比差异主要在声明和初始化的写法上,下面是简单声明一个数组: ...
- INS-40718 和 INS - 30516
RAC 安装的时候报错, INS-40718 这个是自己填写的 scan name 和 /etc/hosts 里定义的不一致 可以cat /etc/hosts 看一下 INS - 30 ...
- springboot+junit测试
文章目录 一.junit断言 二.测试模块 三.使用Mockito作为桩模块 四.使用mockMvc测试web层 五.批量测试和测试覆盖率 参考视频:用Spring Boot编写RESTful API ...
- Java中Map的entrySet()详解
转发:原博客 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系.Map中采用Entry内部类来表示一个映射项,映射项包含Key和ValueMap.Entry里面包含getKey() ...
- DJANGO-天天生鲜项目从0到1-013-订单-支付宝支付
本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...
- 使用iOS网络请求
https://github.com/yuantiku/YTKNetwork/blob/master/Docs/2.0_MigrationGuide_cn.md
- 数据库(十二):pymysql
进击のpython ***** 数据库--pymysql 数据库就算是学习完毕了,但是我们学习数据库的本质是什么? 是想让数据库像文件存储一样将信息存储起来供我们调用 那回归本行,我就应该是用pyth ...
- IO流——转换流、缓冲流
一.转换流 1. OutputStreamWriter类 属于字符输出流,OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节. 它的作 ...
- Linux重定向用法详解
大家好,我是良许. 相信大家平时都会有需要复制粘贴数据的时候,如果是打开文件进行复制粘贴,就不可避免的需要较多的鼠标与键盘的操作,就会比较繁琐.那么有没有可以省掉这些繁琐操作的复制粘贴的方法呢? 答案 ...
- Biopython 第三方库示例
Biopython 第三方库示例 https://biopython-cn.readthedocs.io/zh_CN/latest/