Leetcode45: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.
/**
* 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) {
<span style="white-space:pre"> </span>int lenA = 0;
int lenB = 0;
ListNode *head1 = headA;
<span style="white-space:pre"> </span>ListNode *head2 = headB;
while (head1!=NULL)
{
head1 = head1->next;
lenA++;
}
while (head2!=NULL)
{
head2 = head2->next;
lenB++;
}
int n;
if (lenA < lenB)
{
n = lenB - lenA;
head1 = headA;
head2 = headB;
for (int i = 0; i < n; i++)
{
head2 = head2->next;
}
}
else
{
n = lenA - lenB;
head1 = headA;
head2 = headB;
for (int i = 0; i < n; i++)
{
head1 = head1->next;
}
} while (head1 != head2)
{
head1 = head1->next;
head2 = head2->next;
} return head1;
}
};
Leetcode45: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 ...
- LeetCode之“链表”:Intersection of Two Linked Lists
此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...
- LeetCode OJ: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 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- [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 解题思路
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 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
随机推荐
- Docker For MYSQL 8.0 特别注意修复数据库新的验证方式
从Docker登录MySQL的终端 docker exec -it wordpress-mysql /bin/bash 登录数据库 mysql -u root -p 使用MYSQL数据库 use my ...
- Android开发中的日期格式化
下面的转换符来自Java,但是在android中同样可用.(以下表格内容来自互联网.) 常见日期格式化转换符 转换符 说 明 示 例 %te 一个月中的某一天(1-31) 2 %tb 指定语言环 ...
- [LnOI2019]长脖子鹿省选模拟赛 东京夏日相会
这里来一发需要开毒瘤优化,并且几率很小一遍过的模拟退火题解... 友情提醒:如果你很久很久没有过某一个点,您可以加上特判 可以像 P1337 [JSOI2004]平衡点 / 吊打XXX 那道题目一样 ...
- 另一种压缩图片的方法---Machine learning 之 PCA(Principle Component Analysis)
PCA最主要的用途是用来减少特征向量的数目,N个特征向量 减小到 K个特征向量.如果为了可视化,k可以使3 或者 2.这样可以加速算法的学习速度. PCA用来压缩图像同一有效. 具体方式以及原理在gi ...
- 【Leetcode】376. Wiggle Subsequence
Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...
- Assembly之instruction之Register Mode
Assembler Code Content of ROM MOV R10,R11 MOV R10,R11 Length: One or two words Operation: Move the c ...
- tp实现多语言支持测试
用tp框架实现网页多种语言切换 时间:2016-11-11 浏览次数:1120 编辑:youjiejie 网页如何设计多种语言切换,本文用tp框架实现网页多种语言切换方法结合实例形式较为详细的分析 ...
- dapper.net 存储过程
var param = new DynamicParameters(); param.Add(); param.Add(); param.Add(, DbType.Int32, ParameterDi ...
- 函数式编程-将Monad(单子)融入Swift
前言 近期又开始折腾起Haskell,掉进这个深坑恐怕很难再爬上来了.在不断深入了解Haskell的各种概念以及使用它们去解决实际问题的时候,我会试想着将这些概念移植到Swift中.函数式编程范式的很 ...
- AFNetworking源码解析-https证书相关
本篇说说安全相关的AFSecurityPolicy模块,AFSecurityPolicy用于验证HTTPS请求的证书,先来看看HTTPS的原理和证书相关的几个问题. HTTPS HTTPS连接建立过程 ...