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 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
随机推荐
- Dresdon简介
很久没有写文章了.这几年经历了很多事情:离开VMware的不舍,拿到融资的开心,重回VMware的亲切,以及不再争强好胜,只做自己喜欢事情的平和. 可以说,我是幸运的:我这一辈子都在选择,而不是被迫接 ...
- JVM调优工具Arthas的使用
Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱.在线排查问题,无需重启:动态跟踪Java代码:实时监控JVM状态. Arthas 支持JDK6+,支持Linux/Mac/Wind ...
- Guava集合--新集合类型
Guava引入了很多JDK没有的.但我们发现明显有用的新集合类型.这些新类型是为了和JDK集合框架共存,而没有往JDK集合抽象中硬塞其他概念.作为一般规则,Guava集合非常精准地遵循了JDK接口契约 ...
- echarts 实战 : 恼人的间隔问题
使用 echarts 的时候,可能我们需要这个图表的间隔是固定的.比如 3个 4个 5个. (注意计算间隔数量的时候是不算 x轴 本身的.) 这个问题看似简单,其实有点麻烦. yAxis.splitN ...
- STL源码剖析:算法
启 算法,问题之解法也 算法好坏的衡量标准:时间和空间,单位是对数.一次.二次.三次等 算法中处理的数据,输入方式都是左闭又开,类型就迭代器, 如:[first, last) STL中提供了很多算法, ...
- java 之死循环
public class StringTest { public static void main(String[] args) { System.out.println(getStringCount ...
- 前缀和线性基HDU6579
Operation 题解:看到区间最大异或和,首先想到的是线性基: 线性基可以处理的操作是: 在数列末尾插入一个数 查询全局的子集异或最大值 由于线性基的长度很短,因此我们可以将数列所有前缀的线性基保 ...
- Android:自定义BaseActivity基类
使用BaseActivity可以封装一些重复代码例如设置标题栏颜色,封装一些工具类... 主要功能: 封装Toast 新建一个BaseActivity继承自Activity package com.o ...
- 【JMicro】微服务部署example.provider应用
JMicro是一个用Java语言实现的开源微服务全家桶, 源码地址:https://github.com/mynewworldyyl/jmicro, Demo地址:http://124.70.152. ...
- 使用MacOS自带的SVN客户端
原文链接:https://jingyan.baidu.com/article/5552ef479c1554518ffbc92f.html 摘要:mac环境下有自带的SVN服务端和客户端,SVN是许多公 ...