两个链表的交叉

请写一个程序,找到两个单链表最开始的交叉节点。

注意事项

  • 如果两个链表没有交叉,返回null。
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。

样例

下列两个链表:



在节点 c1 开始交叉。

挑战

需满足 O(n) 时间复杂度,且仅用 O(1) 内存。

标签

链表

code

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// write your code here
stack<ListNode *> stackA, stackB;
ListNode * result=NULL,*pA=headA,*pB=headB; if(headA==NULL || headB==NULL)
return result; while(pA != NULL) {
stackA.push(pA);
pA = pA->next;
}
while(pB != NULL) {
stackB.push(pB);
pB = pB->next;
} while(!stackA.empty() && !stackB.empty()) {
if(stackA.top() == stackB.top()) {
result = stackA.top();
stackA.pop();
stackB.pop();
}
else {
break;
}
}
return result;
}
};

LintCode-380.两个链表的交叉的更多相关文章

  1. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  2. 两个链表的交叉 · Intersection of Two Linked Lists

    [抄题]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  3. intersection of two linked lists.(两个链表交叉的地方)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. 查找两个链表的第一个交叉结点(Python实现)

    题目 给定两个单链表,查找这两个单链表的第一个交叉节点. 例如:链表list_a为:a1→a2→c1→c2→c3,链表list_b为:b1→b2→b3→c1→c2→c3.那么它们第一个交叉结点为c1. ...

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

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

  7. 剑指Offer面试题:31.两个链表的第一个公共节点

    一.题目:两个链表的第一个公共节点 题目:输入两个链表,找出它们的第一个公共结点. 链表结点定义如下,这里使用C#语言描述: public class Node { public int key; p ...

  8. 剑指offer七:两个链表的第一个公共结点

    输入两个链表,找出它们的第一个公共结点. import java.util.*; public class Solution { public ListNode FindFirstCommonNode ...

  9. 剑指Offer 两个链表的第一个公共结点

    题目描述 输入两个链表,找出它们的第一个公共结点.   思路: 题目说的很笼统,应该是有2个链表,找出公共点,第一个公共点后面的链表是共同所有的.可以用map做,直接检测map里有没有出现这个节点. ...

随机推荐

  1. 分享一个hybrid框架ionic

    ionic 是一个 HTML5 应用程序开发框架. 可以使用 HTML.CSS 和 Javascript 构建接近原生体验的移动应用程序.具有速度快,界面现代化.美观等特点.下面一起看一下如何使用 安 ...

  2. JS 原型总结

    参考: (从内存角度)简单类型与复杂类型及原型链

  3. Python文本和字符串常用操作

    ## 字符串分割 line = "This is my love!" fields = line.split(' ') print(fields) # ['This', 'is', ...

  4. I2C驱动

    在I2C总线驱动下,也是硬件设备和驱动分离,使以就需要通过它们的名字来匹配,这样驱动的probe函数才能被调用 查看linux内核的Documents目录下的说明文件,可知构造i2c设备有4种方法: ...

  5. ruby配置镜像源

    1.打开电脑的cmd窗口,输入如下命令即可查看gem镜像: gem sources l 或是直接使用 gem sources 查询结果如下: C:\Users\Administrator>gem ...

  6. Leecode刷题之旅-C语言/python-204计数质数

    /* * @lc app=leetcode.cn id=204 lang=c * * [204] 计数质数 * * https://leetcode-cn.com/problems/count-pri ...

  7. C语言自问自答

    Windows系统下,最好如何配置环境? notepad++,tdm-gcc,powershell来进行! scanf函数的返回值,和不符合格式如何返回? #include<stdio.h> ...

  8. C语言判断字符串是否旋转过

    //方法一 //每次左旋一次,判断旋转之后字符串是否与目标字符串是否一致 //旋转一圈 没有找到返回0 #define _CRT_SECURE_NO_WARNINGS #include<stdi ...

  9. 如何制作 Ubuntu 系统的 USB 启动盘

    下载 Ubuntu 打开 http://mirrors.ustc.edu.cn 获取安装镜像 --> 获取 ISO 刻录 Ubuntu 到 U 盘 打开 http://rufus.akeo.ie ...

  10. mongoengine中queryset触发网络访问机制剖析

    背景 最近新上线的一个服务,偶尔会有超时告警,其主要逻辑仅仅只是简单的读/写mongodb,而且服务上线初期,流量并不大,因而理论上来说,每次请求都应该很快才对,事实上分析日志也证实90%以上的请求都 ...