两个链表的交叉

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

注意事项

  • 如果两个链表没有交叉,返回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. 纯js轮播图练习-2,js+css旋转木马层叠轮播

    基于css3的新属性,加上js的操作,让现在js轮播图花样越来越多. 而现在出现的旋转木马层叠轮播的轮播图样式,却是得到了很多人都喜爱和投入使用. 尤其是在各大软件中,频繁的出现在大家的眼里,在web ...

  2. spring-mvc高级技术

    Spring MVC高级技术包括但不限于web.xml配置.异常处理.跨重定向请求传递数据 1.web.xml文件的配置 <!DOCTYPE web-app PUBLIC "-//Su ...

  3. C++函数调用之——值传递、指针传递、引用传递

    1.简介 1.值传递:形参时实参的拷贝,改变函数形参并不影响函数外部的实参,这是最常用的一种传递方式,也是最简单的一种传递方式.只需要传递参数,返回值是return考虑的:使用值传递这种方式,调用函数 ...

  4. PAT A1060 (Advanced Level) Practice

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...

  5. Python写网络后台脚本

    Python写网络后台脚本. 首先安装Python3.6.5,在centos中自带的Python是2.6版本的,现在早就出现了3.6版本了况且2和3 之间的差距还是比较大的,所以我选择更新一下Pyth ...

  6. python3 urllib爬取wallhalla网站图片

    点我去我的github上看源码 简单使用静态方法爬取https://wallhalla.com/网站的图片 参考: https://blog.csdn.net/cquptcmj/article/det ...

  7. 牛客小白月赛4C——病菌感染

    链接:https://www.nowcoder.com/acm/contest/134/C 来源:牛客网 #include <bits/stdc++.h> using namespace ...

  8. Java设计模式(6)——创建型模式之原型模式(Prototype)

    一.概述 概念 // 引用自<Java与模式> UML图 第二种:登记式 二.实践 先导知识 对象的拷贝: 直接赋值:此时只是相当于a1,a2指向同一个对象,无论哪一个操作的都是同一个对象 ...

  9. 成都Uber优步司机奖励政策(3月14日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. jQuery File Upload 文件上传插件使用一 (最小安装 基本版)

    jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...