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.

问题:判断两个列表是否有相交的元素,若有,找出相交节点。

这题也是一道基础题,看了自己的列表知识还需要巩固下才好。

分别求出两个列表的长度 len1, len2 ,以及他们的长度差异 diff

跳过长度差异部分,对于剩余的相同长度部分,依次检查两个链表的对应节点,若又存在相交节点,则必有两个对应节点相等。

     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {

         int len1 = ;
ListNode* p1 = headA;
while(p1 != NULL){
p1 = p1->next;
len1++;
} int len2 = ;
ListNode* p2 = headB;
while(p2 != NULL){
p2 = p2->next;
len2++;
} p1 = headA;
p2 = headB;
if (len1 > len2){
int diff = len1 - len2;
while(diff > ){
p1 = p1->next;
diff--;
}
} if (len2 > len1){
int diff = len2 - len1;
while(diff > ){
p2 = p2->next;
diff--;
}
} while(p1 != NULL ){
if ( p1 == p2){
return p1;
}
p1 = p1->next;
p2 = p2->next;
} return NULL;
}

参考资料:

LeetCode: Intersection of Two Linked Lists 解题报告, Yu's garden

[LeetCode] 160. Intersection of Two Linked Lists 解题思路的更多相关文章

  1. [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 ...

  2. 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...

  3. Java for 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 ...

  4. Java [Leetcode 160]Intersection of Two Linked Lists

    题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. (链表 双指针) 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 ...

随机推荐

  1. js中内建对象

    JavaScript charAt() 函数 string.charAt(n) -- 返回指定位置(n)的字符 char是character的缩写,中文"符号,字符" 引用网址:h ...

  2. 泛泰A870刷4.4专用英文版非触摸CWM Recovery 6.0.4.8(三版通刷)

    首先声明. 发此Recovery的目的是測试能否够三版都能够启动. 而且不会出现像850 860之前出现过的卡第一屏问题! 不希望看到某些人士的过度解读!! 此Recovery能够刷第三方4.4 RO ...

  3. web服务交互中HTTP数据内容GZIP,ZLIB格式压缩与解压缩封装(共享)

    点击下载独立的dll //dll内部封装API格式 //gzip BOOL fnZlibDecompressPacket (__IN_PARAM unsigned char* gZlibDataBuf ...

  4. 几个常用的ps命令

    1. ps aux If you are looking for a short summary of the active processes, use  ps aux [root@rhel7 tm ...

  5. thinkphp3.2.3 成功对接支付宝接口

    一.首先下载支付宝官方接口,下载地址: https://b.alipay.com/order/productDetail.htm?productId=2012111200373124&tabI ...

  6. ASP.NET-FineUI开发实践-7

    下拉显示grid列表.其实很简单,但是试了很多方法,水平有限,主要是都不好使,还是简单的好使了,分享下. 先是看了看网上的,是直接写个了extjs控件类(我也不懂),然后直接用就行了,要写成FineU ...

  7. java沙箱机制原理

    参考文档如下: http://www.2cto.com/kf/201012/79578.html

  8. Spring.net--很棒的事务处理

    1--Case 比如t_Order订单表1,t_OrderDetail订单明细表2 下一张订单会往表1插入一条数据,表2会插入多行数据 使用Spring.net事务管理 例如 ---Order---订 ...

  9. 您为这个网络适配器输入的IP地址xxx.xxx.xxx.xx已经分配给另一个适配器xxx...

    您为这个网络适配器输入的IP地址xxx.xxx.xxx.xx已经分配给另一个适配器‘xxx NIC’.... 2008年11月03日 星期一 08:51 问题现象:   在网卡的TCP/IP属性中无法 ...

  10. 重装eclipse要做的事

    当我们要在新环境上安装eclipse时,往往会做很多的个性修改和安装一些插件,下面就这些做一下总结: 一.插件 1.svn插件(subclipse) 插件官网下载地址:http://subclipse ...