LeetCode C++ 160. Intersection of Two Linked Lists【Linked List/Two Pointers】简单
发布日期:2021-07-01 02:56:42
浏览次数:2
分类:技术文章
本文共 2699 字,大约阅读时间需要 8 分钟。
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
begin to intersect at node c1.
Example 1:
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3Output: Reference of the node with value = 8Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
Example 2:
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1Output: Reference of the node with value = 2Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
Example 3:
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2Output: nullInput Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.Explanation: The two lists do not intersect, so return null.
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.
- Each value on each linked list is in the range
[1, 10^9]
. - Your code should preferably run in
O(n)
time and use onlyO(1)
memory.
题意:编写一个满足 O(n)
时间复杂度、且仅用 O(1)
内存的程序,找到两个单链表相交的起始节点(注意,整个链表结构中没有环)。如果两个链表没有交点,返回 null
。在返回结果后,两个链表仍须保持原有的结构。
解法 双指针
这里我们定义两个指针。第一轮让两个指针不断前进,到达末尾节点后使其指向另一个链表的头部。接着是第二轮移动,如果相遇则为交点,有交点就返回;无交点则最终两个指针都等于 nullptr
,它们相等从而退出循环。
class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if (headA == nullptr || headB == nullptr) return nullptr; //无交点 ListNode *pa = headA, *pb = headB; while (pa != pb) { pa = (pa == nullptr) ? headB : pa->next; pb = (pb == nullptr) ? headA : pb->next; } return pa; }};
为什么这样做是正确的?因为两个指针都完成第一轮移动后,恰好抹除了长度差。以下图为例,第一轮移动后,pb
先完成第一轮移动并指向 0
,此时 pa
指向 2
;当 pa
完成第一轮移动并指向 3
时,pb
指向 1
。显然,它们再次移动,一定会相遇。
执行用时:48 ms, 在所有 C++ 提交中击败了94.76% 的用户内存消耗:14.7 MB, 在所有 C++ 提交中击败了16.03% 的用户
转载地址:https://memcpy0.blog.csdn.net/article/details/109733825 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月26日 14时24分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
TestNG 学习总结 - 忽略测试(八)
2019-05-03
TestNG 学习总结 - 分组执行测试(九)
2019-05-03
TestNG 学习总结 - TestNG运行JUnit测试(十三)
2019-05-03
TestNG 学习总结 - 测试结果报告(十四)
2019-05-03
TestNG 学习总结 - 测试结果报告 - HTML和XML报告(十七)
2019-05-03
TestNG 学习总结 - 测试结果报告 - Junit报告(十八)
2019-05-03
JVM的内存区域划分
2019-05-03
Java对象深复制、浅复制
2019-05-03
细分自动化测试
2019-05-03
使用WebDriver完成web页面切换操作(附源码)
2019-05-03
使用webdriver中的JavascriptExecutor执行js改变DOM属性
2019-05-03
调研 中央空调 地暖 水暖
2019-05-03
谈创新和效率,如何总结分享. 归类,几大类
2019-05-03
架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级tu)
2019-05-03
test 博客园功能 和 搜索 seo 能力
2019-05-03
待学习
2019-05-03
山东科技大学2015-2016学年第一学期程序设计基础期末考试第一场 题解
2019-05-03
Python教程-----引用模块
2019-05-03
山东科技大学2020年4月9日题解
2019-05-03