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 ...
随机推荐
- 次短路poj3463
poj3463大意:统计最小的长度个数+统计最小的长度+1的个数,大概就是求最短路和次短路的条数更新的时候有5种情况,有个细节就是它得是二维的,一个表示节点编号,一个0/1表示它是次短路的还是最短路的 ...
- 常用的git操作命令
整理来源于廖雪峰的git教程https://www.liaoxuefeng.com git: 分布式版本控制系统 本地有完整的代码库,还有远程代码库 svn: 集中式版本控制系统 必须联网时才可提交 ...
- Android SDK上手指南:知识测试
Android SDK上手指南:知识测试 2014-01-22 10:00 核子可乐 译 51CTO 字号:T | T 在从零开始学习Android开发系列教程当中,我们已经了解了为Android平台 ...
- 洛谷P2827 蚯蚓
传送门 pts85/90(90应该是个意外,第一次交是90之后都是85了): 优先队列模拟题意 #include<iostream> #include<cstdio> #inc ...
- T2485 汉诺塔升级版(普及)(递归)
https://www.luogu.org/problem/show?pid=T2485 题目背景 汉诺塔升级了 题目描述 现在我们有N个圆盘和N个柱子,每个圆盘大小都不一样,大的圆盘不能放在小的圆盘 ...
- UVA11722 Jonining with Friend
Joining with Friend You are going from Dhaka to Chittagong by train and you came to know one of your ...
- 图像通道、Scalar、分离、合成通道
http://lib.csdn.net/article/opencv/33264 http://blog.csdn.net/laohu_tiger/article/details/17359777 h ...
- Django项目:CRM(客户关系管理系统)--22--14PerfectCRM实现King_admin分页的省略显示
{#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...
- 关系型数据库和nosql非关系型数据库
快下班了.突然和同事聊起了node+mongdb,因为我们这里的项目没有mongdb,就问后端的同事,我们'爱装逼'的后端的同事让我们先自己学习一下什么关系型数据库和非关系型数据库.一顿百度查询了解下 ...
- 使用nodejs安装http-server
一.下载nodejs(https://nodejs.org/) 二.在环境变量中配置nodejs路径: path: D:\Program\nodejs\ 三.打开终端: 使用node -v测试node ...