问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 的时间复杂度为: 。

最新文章

  1. 《Linux内核设计与实现》课本第五章学习笔记——20135203齐岳
  2. DotNetOpenAuth使用笔记
  3. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体平移)
  4. 反向Ajax,实现服务器向客户端推送消息之 Comet
  5. Nginx 403 forbidden的解决办法
  6. codevs 1107 等价表达式
  7. 【翻译】在Ext JS 5应用程序中怎样使用路由
  8. H5滑条(input type=range)
  9. M2阶段事后总结
  10. Linux初学笔记---关于进程管理等
  11. kepware http接口 c语言 ruby
  12. 购物商城学习--第二讲(maven工程介绍)
  13. [A类会议] 国内论文检索
  14. 学 Win32 汇编[28] - 跳转指令: JMP、JECXZ、JA、JB、JG、JL、JE、JZ、JS、JC、JO、JP 等
  15. python WEB UI自动化在日期框中动态输入当前日期
  16. codevs 1001 舒适的线路 kruskal/gcd
  17. Thinkphp --- 入口文件
  18. oracle goldengate安装
  19. 设计模式PHP篇(二)————工厂模式
  20. 如何简单解释 MapReduce算法

热门文章

  1. 数据结构C语言实现----出队伍操作
  2. 终于搞懂Spring中Scope为Request和Session的Bean了
  3. eclipse GIT本地库分支操作
  4. webpack 编译时,提示 Unexpected token: keyword &#171;const&#187;
  5. spring学习(七)spring整合JDBC
  6. Python环境那点儿事(Windows篇)
  7. VMware虚拟机黑屏解决
  8. Hbase1.2.3安装
  9. 解释Crypto模块,No module named &quot;Crypto&quot;
  10. Maven中出现Could not find artifact ...:pom:0.0.1-SNAPSHOT