[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 ...
随机推荐
- android onIntent 是什么东西
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...
- android 获取屏幕尺寸
文章转载自:http://blog.csdn.net/congqingbin/article/details/7474276// 通过WindowManager获取 DisplayMetrics dm ...
- ScriptManager需要用到的JS
<script type="text/javascript"> Sys.Application.add_load(function() { var form = Sys ...
- ASP.NET MVC 自定义错误页面心得
自定义错误页面的目的,就是为了能让程序在出现错误/异常的时候,能够有较好的显示体验. 所以,首先要先了解,我们可以在哪里捕获异常. 当程序发生错误的时候,我们可以在两个地方捕获: Global里面的A ...
- DOM重绘对focus的影响
在处理获取焦点时一直不能获取到. 搜索了下资料是因为 当DOM的变化影响了元素的几何属性(宽和高),浏览器需要重新计算元素的几何属性,同样其他元素的几何属性和位置也会因此受到影响. 重排:浏览器会使渲 ...
- VB.NET Shared(共享)和 Static(静态)关键字的区别
共享成员(Shared): VB.NET现在是支持真正的面向对象编程,可以继承.使用多态.共享成员 和静态成员. 共享成员就是在所有类和所定义派生类的实例之间共享的方法.属 性.字段和事件.所有使用类 ...
- ubuntu安装jira步骤
背景:前些日子在原来的一台云主机上已经部署了一个jira系统,使用一段时间后发现jira占用很多的系统资源,导致主机上的其他服务无法正常工作,于是老大新注册了一个云主机专门用于运行jira,可见公司对 ...
- Android 屏蔽ScrollView滑动操作
屏蔽ScrollView滑动操作,如下,会用到ViewConfiguration这个类,这个类可以获取到用户是否为滑动操作的临界值. 代码如下: package com.xx.uikit.view; ...
- Apache无法启动解决方案
Apache无法启动错误原因: 原因一:80端口占用例如IIS,另外就是迅雷.我的apache服务器就是被迅雷害得无法启用! 原因二:软件冲突装了某些软件会使apache无法启动如Dr.com 你打开 ...
- DedeCms 建站随笔(一)
站名:524社区网 代码:DedeCms织梦5.7代码(UTF-8) 服务器:Linux 问题一:火车头(熊猫)采集,登录成功之后无法获取栏目列表. 原因:1.新建栏目后没有更新栏目HTML文件,并且 ...