160. Intersection of Two Linked Lists (List;Two-Pointers)
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访问到结尾后付给链表B的头指针,同理对链表B操作,从而在第二遍遍历时必定是同步的。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB; while(curA && curB){
if(curA == curB){
return curA;
} curA = curA->next;
curB = curB->next; }
if(!curA && !curB) return NULL;
else if(!curA){
curA = headB;
while(curB){
curB = curB->next;
curA = curA->next;
}
curB = headA;
}
else{
curB = headA;
while(curA){
curB = curB->next;
curA = curA->next;
}
curA = headB;
} while(curA){
if(curA == curB){
return curA;
} curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
160. Intersection of Two Linked Lists (List;Two-Pointers)的更多相关文章
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- [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 ...
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 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 ...
- 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 ...
- [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 ...
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 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 ...
- Java for 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 ...
随机推荐
- keepalived nginx 双机热备图文讲解
http://blog.csdn.net/wanglei_storage/article/details/51175418
- leetcode999
class Solution: def numRookCaptures(self, board: 'List[List[str]]') -> int: basei = 0 basej = 0 r ...
- Human Interface Device (HID) Class Decoder
http://www.usblyzer.com/usb-human-interface-device-hid-class-decoder.htm Human Interface Device (H ...
- TFDStoredProc执行sql server的部分存储过程报错,有的是好的。
TFDStoredProc执行sql server的部分存储过程报错,有的是好的. Invalid character value for cast specification 暂时无解.用fdque ...
- 最详细安装Esxi
https://www.vmware.com/cn/products/vsphere-hypervisor.html Exsi 是一款虚拟化系统,与VMware,VirtualBox不同,它不需要安装 ...
- JAVA版开源微信管家—JeeWx捷微3.1小程序版本发布,支持微信公众号,微信企业号,支付窗
支持小程序,JeeWx捷微3.1小程序版本发布^_^ JeeWx捷微V3.1--多触点小程序版本管理平台(支持微信公众号,微信企业号,支付窗) JeeWx捷微V3.1.0版本紧跟微信小程序更新,在原有 ...
- OC copy mutableCopy, 浅拷贝,深拷贝
copy与mutableCopy都是深拷贝,区别是mutableCopy拷贝出的对象是可变的. OC对象基本都是通过指针访问,所以一般情况下,通过对指针的赋值都是浅拷贝,即只是拷贝了一份对象的指针,对 ...
- ubuntu交换Caps 和 ESC
https://askubuntu.com/questions/363346/how-to-permanently-switch-caps-lock-and-esc This will allow y ...
- js 迭代器 解说
这里要说的是迭代器,是一种思路而已,代码相对来不是最关键的,个人认为,最关键的部分是实现的思路 要求: 在一个网页中,将所有的 p 元素的内容进行替换,但是在特定的位置的 p 元素是要有差异的进行替换 ...
- 自己写一个spring boot starter
https://blog.csdn.net/liuchuanhong1/article/details/55057135