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. 白鹭引擎 - 遮罩( Rectangle )

    1: 矩形遮罩 class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用 ...

  2. 文件系统(File System)

    什么是文件系统,引用百科解释: 操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统. 文件系统是操作系统核心的组成部分,没有它我们无法完成对文件的增.删.改.查等基本操作 概念 ...

  3. vue-router 动态导航 router-link :to属性

    经常碰到这类需求,从后台获取数据后再前程连接,参数id动态获取 <el-row v-for="item in Travels"> <el-col :span=&q ...

  4. EasyARM-iMX283A的Linux 开发环境构建

    Vim的安装 sudo apt-get install vim 等待安装完成后我们来配置简单配置vim的配置文件 vim /etc/vim/vimrc (备注:自己一个人使用的^-^) 在原来的基础上 ...

  5. [转载]Core Elements of a Program

    原文链接 http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-c ...

  6. 2018SDIBT_国庆个人第一场

    A - Turn the Rectangles CodeForces - 1008B There are nn rectangles in a row. You can either turn eac ...

  7. Linux:写一个简单的服务器

    开始了新篇章:Linux网络编程. 基础知识: 套接字概念 Socket本身有"插座"的意思,在Linux环境下,用于表示进程间网络通信的特殊文件类型.本质为内核借助缓冲区形成的伪 ...

  8. 使用ab对网站进行压力测试

    1.安装yum install httpd-tools 2.ab -kc 1000 -n 1000 http://localhost/ab.html 这个指令会使用1000个并发,进行连接1000次

  9. suse 奇怪的crash 问题

    最近遇到一个suse的crash 问题: 我没有使用 mkswap /dev/磁盘路径 来制作swap分区,我有很多剩余内存,我设置nr_swapfiles为0,可是我还是遇到了关于swap的cras ...

  10. 【384】reduce归纳、map映射、filter筛选 的用法

    参考:4. Map, Filter and Reduce — Python Tips 0.1 documentation 参考:Python的functools.reduce用法 Map:映射,对于列 ...