Intersection of Two Linked Lists两链表找重合节点
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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
/**
* 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) {
int lenA=,lenB=;
ListNode *tempA=headA;
ListNode *tempB=headB;
while(tempA){
lenA++;
tempA=tempA->next;
}
while(tempB){
lenB++;
tempB=tempB->next;
}
while(lenA>lenB){
headA=headA->next;
lenA--;
}
while(lenB>lenA){
headB=headB->next;
lenB--;
}
while(headA != headB){
headA=headA->next;
headB=headB->next;
}
return headA; }
};
Intersection of Two Linked Lists两链表找重合节点的更多相关文章
- [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 ...
- intersection of two linked lists.(两个链表交叉的地方)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 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 ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
- 160 Intersection of Two Linked Lists 相交链表
编写一个程序,找到两个单链表相交的起始节点.例如,下面的两个链表:A: a1 → a2 ↘ ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
随机推荐
- CodeForces - 752B
CodeForces - 752Bhttps://vjudge.net/problem/597648/origin简单模拟,主要是细节特殊情况多考虑一下,看代码就行 #include<iostr ...
- Python读写文件学习笔记
一. 基础 1.创建文件夹 import os os.makedirs('I:\\pythonWorkPace') # 创建文件夹 2. 获取文件夹里面文件列表 import os # os.make ...
- Office2010 破解(Microsoft Toolkit 2.4.3.exe)
这两天破解刚安装好的office2010,总是报错 刚才重新下载了Microsoft Toolkit 2.4.3.exe工具后,破解成功,操作如下: 选择office按钮后,如下操作,
- 【DM8168学习笔记2】DM8168 EZSDK 结构
1 2 3 4 5
- Newtonsoft.json 二次引用出错解决办法
一.一般在C# 项目中二次引用会出现如下错误: 解决办法:用编辑器打开项目下的文件(*.csproj),可以找到在这个文件中,Newtonsoft.Json的引用,删掉引用,然后在项目中重新引用就可以 ...
- HDU2896 病毒侵袭 AC自动机模板
各种MLE,这模板感觉有问题,next数组开128也会MLE,实际上可见字符为编号32~126,只用开100就行. #include <iostream> #include <cst ...
- PHP苹果推送实现(APNS)
以下资料网上收集整理得来 1.在ios dev center制作相关证书和文件用客户端实现(不再赘述,网上很多,) 网上教程: http://blog.csdn.net/lizhenning87/ar ...
- CentOS 6.5安装libvirt启动不了libvirtd进程的错误
今天安装kvm时遇到了一个libvirtd服务启动不了的问题,具体报错信息如下: 自己检查了一阵子,确定其他方面没有问题,在网上搜到了答案,在此整理下来: 再次启动服务,没有任何问题:
- 【html、CSS、javascript-10】jquery-操作元素(属性CSS和文档处理)
一.获得内容及属性 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val ...
- Unknown command: crawl
Use "scrapy" to see available commands 1.使用命令行方式cmd,是因为没有cd到项目的根目录,crawl会去搜索cmd目录下的scrapy. ...