380. Intersection of Two Linked Lists【medium】
Write a program to find the node at which the intersection of two singly linked lists begins.
Notice
- 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.
The following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
Your code should preferably run in O(n) time and use only O(1) memory.
解法一:
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
if(headA == NULL || headB == NULL)
return NULL;
ListNode* iter1 = headA;
ListNode* iter2 = headB;
int len1 = ;
while(iter1->next != NULL)
{
iter1 = iter1->next;
len1 ++;
}
int len2 = ;
while(iter2->next != NULL)
{
iter2 = iter2->next;
len2 ++;
}
if(iter1 != iter2)
return NULL;
if(len1 > len2)
{
for(int i = ; i < len1-len2; i ++)
headA = headA->next;
}
else if(len2 > len1)
{
for(int i = ; i < len2-len1; i ++)
headB = headB->next;
}
while(headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA;
}
};
380. Intersection of Two Linked Lists【medium】的更多相关文章
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- 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 ...
- [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 ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
随机推荐
- Ubuntu下轻松安装virtualbox
转自:http://blog.csdn.net/flm2003/article/details/8168628 以下假设你的Ubuntu系统版本为11.10的64位版本,进行如下操作: 1.到http ...
- 粗览Activiti Modeler操作和源代码
Activiti Model Editor组件 我的 了解ActivitiExplorer及其Vaadin实现方式博文里提到ActivitiExplorer使用的是Vaadin架构,但是Activit ...
- es6数组去重复
var arr=[1,1,2,3,5,7,7,7] arr=Array.from(new Set(arr))
- Openshift 节点添加和删除
1.节点添加 在新节点上编辑yum源/etc/yum.repo.d/ocp.repo /etc/hosts在主和节点上都加上相应信息 编辑host文件,加入 [OSEv3:children] mast ...
- iOS:导航条滚动透明度随着tableView的滚动而变化
来源:HelloYeah 链接:http://www.jianshu.com/p/b8b70afeda81 下面这个界面有没有觉得很眼熟.打开你手里的App仔细观察,你会发现很多都有实现这个功能.比如 ...
- 【转载】WebStorm安装 & Nodejs-Express Demo
试试下面这个方法: http://www.sdifenzhou.com/?p=6941 从这里下载 http://pan.baidu.com/share/link?shareid=2512210975 ...
- SQL盲注攻击的简单介绍
1 简介 1.1 普通SQL注入技术概述 目前没有对SQL注入技术的标准定义,微软中国技术中心从2个方面进行了描述[1]: (1) 脚本注入式的攻击 (2) 恶意用户输 ...
- Spark2-对于Null/Nan的处理
一.几种查找空值的方法 1.Column方法 column.isNull/column.isNotNull/column.isNaN 2.类sql方法 二.na方法 2.1 na.drop方法 2.1 ...
- Python绘制直方图 Pygal模拟掷骰子
#coding=utf-8 from random import randint class Die(): """骰子类""" def __ ...
- 转: 加快Android编译速度
转: http://timeszoro.xyz/2015/11/25/%E5%8A%A0%E5%BF%ABandroid%E7%BC%96%E8%AF%91%E9%80%9F%E5%BA%A6/ 加快 ...