[Linked List]Intersection of Two Linked Lists
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.
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:int getListLength(ListNode* head){int len = 0;while(head){len++;head=head->next;}return len;}ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {int lenA = getListLength(headA);int lenB = getListLength(headB);ListNode* common = NULL,*startA=headA,*startB=headB;if(lenA<lenB){int diff = lenB-lenA;while(diff--) startB=startB->next;}else{int diff = lenA-lenB;while(diff--) startA=startA->next;}while(startA){if(startA == startB){common = startA;break;}startA=startA->next;startB=startB->next;}return common;} };
转载于:https://www.cnblogs.com/zengzy/p/5041278.html
[Linked List]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 ...
- 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 ...
- Intersection of Two Linked Lists——经典问题
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Intersection of Two Linked Lists 解答
Question Write a program to find the node at which the intersection of two singly linked lists begin ...
- 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 ...
- 【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 ...
- [LeetCode]--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 ...
- 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从零单排】No.160 Intersection of Two Linked Lists
题目 Write a program to find the node at which the intersection of two singly linked lists begins. For ...
最新文章
- LeetCode 804	Unique Morse Code Words--python,java解法
- FD.io/VPP — L2TP
- php删除数据库中数据表的数据,php中删除数据库数据例子
- hadoop集群swap_性能瓶颈--MEM(swap)
- 转:实例学习PHP程序对用户身份认证实现两种方法
- 关于nginx/lighttpd epoll高并发以及apache为何不采用epoll的的疑惑 不指定
- 如何将Webpack与React结合使用:深入的教程
- 2.SOA架构:服务和微服务分析及设计--- 理解面向服务架构
- PHP倒序后五个字符串,并显示
- SHELL下把一个文件附加到另外一个文件,注意编码问题
- Valve员工手册,一部震惊世界的“人生攻略”
- 万能的MATLAB丨大厂工程师必备技能,免费学习
- python制作软件安装包_Python安装包及开发工具
- 人行征信报告(上)——一代征信报告的变量梳理
- Windows 10 操作系统,更新之后无法打开Windows Mobile 设备中心,要么双击无反应。要么正在启动,过会就关闭了。
- 怎样删除usb计算机连接网络打印机驱动,打印机USB连接安装设置及常见问题处理...
- c语言tc2.0编译器下载,c语言编译器|c语言编译器(wintc)_绿茶手机网
- 计算机基础 华师在线,华师在线计算机基础试题答案.doc
- MacBook重启之后,外接显示器不亮的解决方法
- python特效源代码_人工智能python代码实现魔幻换天视频特效