[Linked List]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:
int getListLength(ListNode* head){
int len = ;
while(head){
len++;
head=head->next;
}
return len;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = getListLength(headA);
int lenB = getListLength(headB);
ListNode* common = NULL,*startA=headA,*startB=headB;
if(lenA<lenB){
int diff = lenB-lenA;
while(diff--) startB=startB->next;
}else{
int diff = lenA-lenB;
while(diff--) startA=startA->next;
}
while(startA){
if(startA == startB){
common = startA;
break;
}
startA=startA->next;
startB=startB->next;
}
return common;
}
};
[Linked List]Intersection of Two Linked Lists的更多相关文章
- [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 ...
- 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 ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [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 ...
随机推荐
- MVC学习 (二) Razor语法
MVC的Model层我理解与三层架构的Molde没有区别,都是作为各个层之间的数据沟通桥梁.但是关于Control和View都有一些与传统webform不同的特性. 这里先学习View里所用到的Raz ...
- Android Geocoder(位置解析)
Android中提供GPS定位服务,同时开发者可以对获得的位置信息进行解析,可以获得位置的详细信息. 1.gps定位 在Eclipse中建立android应用程序.android sdk中提供了loc ...
- Android的动画
一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...
- php 函数之 )_each()list()implode()explode()in_array()
<?php /* implode() 把数组组合成字符串 explode() 把字符串分割成数组 in_array() 检测内容是否在数组中 each()把数组元素拆分成新的数组 list() ...
- MYSQL 查看可用的字符集的 2 方法
方法 1. show character set; 方法 2. show collation;
- SQL Server 无法启动的 4 种原因
SQL Server 无法启动的原因定位.首先要知道SQL Server 启动的过程. 第一步: 读取注册表,创建log文件.检测硬件.初始化系统配置. 第二步: 启动系统数据库. 第三步: 准备好网 ...
- java面试题大全-基础方面
Java基础方面: 1.作用域public,private,protected,以及不写时的区别答:区别如下:作用域 当前类 同一package 子孙类 ...
- QtWaitingSpinner
https://github.com/snowwlex/QtWaitingSpinner
- LoggingApplicationListener
org.springframework.boot:spring-boot:1.3.0.M1 spring-boot-1.3.0.M1.jar package org.springframework.b ...
- linux之SQL语句简明教程---WHERE
我们并不一定每一次都要将表格内的资料都完全抓出.在许多时候,我们会需要选择性地抓资料.就我们的例子来说,我们可能只要抓出营业额超过 $1,000 的资料.要做到这一点,我们就需要用到 WHERE 这个 ...