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. Callable和Future 多线程

    参考:https://www.cnblogs.com/fengsehng/p/6048609.html

  2. JDK1.8 LocalDateTime 时间类与字符互转

    public static void main(String[] args) { DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPat ...

  3. TCP连接异常断开检测(转)

    TCP是一种面向连接的协议,连接的建立和断开需要通过收发相应的分节来实现.某些时候,由于网络的故障或是一方主机的突然崩溃而另一方无法检测到,以致始终保持着不存在的连接.下面介绍一种方法来检测这种异常断 ...

  4. ZEOSDBO-7安装

    zeosdbo是一套免费开源的Delphi数据库连接组件,可连接mssql.mysql.sybase.oracle.firebird.sqlite.postgresql等多种数据库.调用方法简单. 连 ...

  5. SIGCHLD信号

    SIGCHLD的产生条件 子进程终止时 子进程接收到SIGSTOP信号停止时 子进程处在停止态,接受到SIGCONT后唤醒时 也就是说:子进程的运行状态发生变化就会发送SIGCHILD信号:这里的意思 ...

  6. python环境和工具

    1.版本问题 python2.X和python3.X是不兼容,所以选择如果选择了2.X版本,那么为了避免兼容性的问题,在以后使用其他python库或者工具时,也需要选择相对应的版本. 下载地址:htt ...

  7. JSFL 禁止脚本运行时间太长的警告

    fl.showIdleMessage(false);

  8. 关于新版OPENWRT拔PPTP的619错或PPTPD无法连接问题笔记

    旧版的openwrt要安装kmod-ipt-nethelper这个包 对于新版,如3.18或4.xx内核的ROM,要安装kmod-nf-nathelper-extra这个包

  9. [Linux]CentOS7搭建Nginx + MySQL + PHP

    ------------------------------------------------------------------------------------- Nginx安装参考地址:ht ...

  10. 关于PHP Notice: A non well formed numeric value encountered, 你知道多少

    ---------------------------------------------------------------------------------------------- A non ...