问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. LeetCode 160: 相交链表 Intersection of Two Linked Lists

    爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...

  2. [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 ...

  3. LeetCode刷题总结-栈、链表、堆和队列篇

    本文介绍LeetCode上有关栈.链表.堆和队列相关的算法题的考点,推荐刷题20道.具体考点分类如下图: 一.栈 1.数学问题 题号:85. 最大矩形,难度困难 题号:224. 基本计算器,难度困难 ...

  4. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  5. C#LeetCode刷题之#707-设计链表(Design Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...

  6. C#LeetCode刷题之#234-回文链表(Palindrome Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3905 访问. 请判断一个链表是否为回文链表. 输入: 1-> ...

  7. C#LeetCode刷题之#141-环形链表(Linked List Cycle)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3901 访问. 给定一个链表,判断链表中是否有环. 进阶: 你能否 ...

  8. LeetCode刷题之合并排序链表

    合并两个有序链表并返回一个新的列表.新列表应该由连接在一起的节点前两个列表 给定实例:Input: 1->2->4, 1->3->4Output: 1->1->2- ...

  9. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

随机推荐

  1. OSCP Learning Notes - Post Exploitation(2)

    Windows Post Exploitation Target Server: IE8-Win 7 VM 1. Download and upload the fgdump, PwDump7, wc ...

  2. MacOS中Mysql设置默认字符集

    一.查看字符集 mysql> show variables like 'character%'; +--------------------------+-------------------- ...

  3. Arrays.sort() ----- TimSort

    Arrays.sort() Arrays.sort()对于基本类型使用的是DualPivotQuicksort双轴快速排序,而对于非基本类型使用的是TimSort,一种源自合并排序和插入排序的混合稳定 ...

  4. xss原理绕过防御个人总结

    xss原理 xss产生的原因是将恶意的html脚本代码插入web页面,底层原理和sql注入一样,都是因为js和php等都是解释性语言,会将输入的当做命令执行,所以可以注入恶意代码执行我们想要的内容 x ...

  5. Java方法(函数)

    4.1方法简介 方法是语句的集合,他们在一起执行一个功能: 1.方法是解决一类问题的步骤的有序组合(功能块) 2.方法包含于类与对象中 3.方法在程序中创建,在其它地方引用 4.原子性:单一职能原则( ...

  6. 旧的成功的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="htt ...

  7. Laragon修改配置快速创建其他框架的项目

    配置方式 依葫芦画瓢,如添加thinkPHP: # Thinkphp Thinkphp 3.2=composer create-project topthink/thinkphp %s Thinkph ...

  8. java 心跳机制

    心跳机制:就是每隔几分钟发送一个固定信息给服务端,服务端收到后回复一个固定信息如果服务端几分钟内没有收到客户端信息则视客户端断开. 心跳包 心跳包就是在客户端和服务器间定时通知对方自己状态的一个自己定 ...

  9. java 控制语句、数组、方法

    一.控制语句 1.if 语句 if语句是指如果满足某种条件,就进行某种处理. 流程图: 2. if…else语句 语法格式: if (判断条件){ 执行语句1 …… }else{ 执行语句2 …… } ...

  10. jmeter 命令行模式(非GUI)运行脚本,察看结果树结果为空,解决办法;

    jmeter的bin目录下,打开命令窗口,执行jmeter -n -t jmeter脚本 -l 结果: 执行结束后,聚合报告打开结果,显示错误率100%:察看结果树中打开结果,显示无数据: 解决办法: ...