[Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10109542.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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.
编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
A: a1 → a2↘c1 → c2 → c3↗ B: b1 → b2 → b3
在节点 c1 开始相交。
注意:
- 如果两个链表没有交点,返回
nil
. - 在返回结果后,两个链表仍须保持原有的结构。
- 可假定整个链表结构中没有循环。
- 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
1 class Solution { 2 func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode? ) -> ListNode? { 3 if headA == nil || headB == nil { 4 return nil 5 } 6 7 var a = headA 8 var b = headB 9 while (a !== b) { 10 if a != nil { 11 a = a?.next 12 } else { 13 a = headB 14 } 15 16 if b != nil { 17 b = b?.next 18 } else { 19 b = headA 20 } 21 } 22 return a 23 } 24 25 public class ListNode: Equatable { 26 27 public var val: Int 28 public var next: ListNode? 29 30 public init(_ val: Int) { 31 self.val = val 32 self.next = nil 33 } 34 35 public static func ==(lhs: Solution.ListNode, rhs: Solution.ListNode) -> Bool { 36 return lhs.val == rhs.val && lhs.next == rhs.next 37 } 38 } 39 }
转载于:https://www.cnblogs.com/strengthen/p/10109542.html
[Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists相关推荐
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Leetcode160. 相交链表
Leetcode160. 相交链表 题目: 编写一个程序,找到两个单链表相交的起始节点. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = ...
- LeetCode(160): Intersection of Two Linked Lists
Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...
- C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3824 访问. 编写一个程序,找到两个单链表相交的起始节点. 例如 ...
- 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- 47 - 算法 - Leetcode-160 -相交链表
// 链表对齐 - 160 相交链表class Solution {public:ListNode *getIntersectionNode(ListNode *headA, ListNode *he ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 160. 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 OJ 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
最新文章
- Cow Digit Game(博弈论:sg函数)
- Linux编程手册读书笔记第五章(20140408)
- 【Quartz】深入Job、JobDetail、JobDataMap、Trigger
- 论文浅尝 | Zero-Shot Transfer Learning for Event Extraction
- 漫步最优化二十三——一维优化
- 关于企业费控管理的这些陷阱,你知道吗?
- ACM-ICPC 2017 Asia Xi'an A XOR (线性基+线段树思想)
- 几岁孩子可以学python_编程python适合多大孩子学
- 渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
- 5G无线技术基础自学系列 | 5G基站形态演进
- freebsd协议栈学习
- java月历_Java 日历
- 计算机相关论文摘要,计算机类论文摘要
- JAVA中数字转大写字母_java实现数字转大写
- 利用代码实现山脊线、山谷线的提取(arcpy版)
- JS高级进阶总结day01---面向对象编程介绍,new的工作原理以及构造函数,原型对象,实力函数三者之间的关系
- linux命令--mkdir 与文件权限
- C/S软件架构设计模式
- 我的leetcode之旅--万事开头难
- 淘宝/Tmall商品详情页视频数据接口(视频数据,销量数据,sku属性数据,页面上有的数据均可以拿到,支持高并发)
热门文章
- [转]Oracle11g链接提示未“在本地计算机注册“OraOLEDB.Oracle”解决方法
- NHibernate ConfORM Mapping
- Arcgis10.2安装与解决 [转载自麻辣GIS]
- 【LeetCode】198.打家劫舍
- vue代理配置(vue+django前后端分离项目)
- hexo+markdown添加本地图片无法显示
- window系统安装hexo
- oracle 安装程序异常终止,Oracle安装错误“程序异常终止
- mysql语句使用_MySQL语句的使用
- onpostexecute 中更新adapter 事变_Spring面试中常见的9种设计模式!