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 ...
随机推荐
- Element-UI 笔记
1.表单验证 官官方地址 : https://element.eleme.cn/#/zh-CN/component/form 使用rules进行表单字段验证 https://blog.csd ...
- A - Supermarket
Problem description We often go to supermarkets to buy some fruits or vegetables, and on the tag the ...
- ABP的一些特性 (Attribute)
大家应该很熟悉Attribute这个东西吧,ABP里面扩展了一些特性,做过滤权限,返回内容等进行控制,在这里小记下,方便后续查看. [DontWrapResult] //ABP默认对返回结果做了封装 ...
- Java上传视频
页面: 上传文件时的关键词:enctype="multipart/form-data" <%@ page language="java" import=& ...
- jQuery 滑动及点击切换效果
效果图如下: 初始化 hover效果:滑动menuitem,‘首页’不变,字体颜色改变,有下划线展示. 即在动态添加boder-bottom,改变字体颜色颜色 .menuItem:hover{ bor ...
- 努比亚 Z17 mini s (Nubia NX589J) 解锁BootLoader 并刷入recovery ROOT
首先下载好工具链接:链接:https://pan.baidu.com/s/1gher4T9 密码:rypn 备用下载链接:https://pan.baidu.com/s/1nxdzt9Z 本篇教程教你 ...
- hibernate_04_hbm.xml介绍
先贴上类文件Students.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC & ...
- DNN:windows使用 YOLO V1,V2
本文有修改,如有疑问,请移步原文. 原文链接: YOLO v1之总结篇(linux+windows) 此外: YOLO-V2总结篇 Yolo9000的改进还是非常大的 由于原版的官方YOLOv ...
- 图像局部显著性—点特征(SURF)
1999年的SIFT(ICCV 1999,并改进发表于IJCV 2004,本文描述):参考描述:图像特征点描述. 参考原文:SURF特征提取分析 本文有大量删除,如有疑义,请参考原文. SURF对SI ...
- InnoDB undo log物理结构的初始化
水平有限,如果有误请指出.一直以来未对Innodb 的undo进行好好的学习,最近刚好有点时间准备学习一下,通过阿里内核月报和自己看代码的综合总结一下.本文环境: 代码版本 percona 5.7.2 ...