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)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Appium -选择、操作元素2

    选择元素的方法 根据xpath 在Appium中,我们没法使用css,因为css是web专用的 Appium支持xpath来定位元素 对于一些比较复杂的元素的定位,我们可以用它 driver.find ...

  2. java byte[]与十六进制字符串相互转换

    http://blog.csdn.net/worm0527/article/details/69939307 http://blog.csdn.net/androiddeveloper_lee/art ...

  3. 2018SDIBT_国庆个人第五场

    A - ACodeForces 1060A Description Let's call a string a phone number if it has length 11 and fits th ...

  4. cxgrid合并值相同的某列

    设置 cxGrid 的某列的 CellMerging 属性可使这一列相同值的单元格合并. 1)cxGridDBTableViewColumn1.Options.CellMerging:=true  2 ...

  5. Unit 1 overview of IT Industry

    Unit 1 overview of IT IndustryConcept LearningIT Industry OutlookThe term technology commonly refers ...

  6. 【380】python 获取列表排序后的索引列表

    参考:Equivalent of Numpy.argsort() in basic python? - Stack Overflow 通过 enumerate 实现 [i for i,v in sor ...

  7. Statemnet和PerparedStstemnent有哪些区别

    Statement 和 PreparedStatement之间的关系和区别.     关系:PreparedStatement继承自Statement,都是接口     区别:PreparedStat ...

  8. iptables学习

    droidwall.sh #!/system/bin/sh IPTABLES=iptables BUSYBOX=busybox GREP=grep ECHO=echo # Try to find bu ...

  9. hadoop /etc/Hadoop/slaves文件解析【转】

    slaves文件里面记录的是集群里所有DataNode的主机名,到底它是怎么作用的呢?slaves文件只作用在NameNode上面,比如我在slaves里面配置了 host1 host2 host3 ...

  10. opencv输出图片像素值

    需求:在控制台输出灰度图像的像素值 代码: #include <stdio.h> #include <iostream> #include <opencv2/core/c ...