题目:

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.

思路:求一个单链表交集的首结点

  方法一:找出两个链表的长度差n,长链表先走n步,然后同时移动,判断有没有相同结点

  方法二:两个链表同时移动,链表到尾部后,跳到链表头部,相遇点即为所求

代码:

方法一:

 /**
* 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) {
//方法一
ListNode *p = headA;
ListNode *q = headB;
if(!p || !q) return NULL;
while(p && q && p != q)
{
p = p->next;
q = q->next;
if(p == q)
return p;
if(!p)
p = headB;
if(!q)
q = headA;
}
return p;
}
};

方法二:

 /**
* 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) {
//方法二
if (headA == NULL || headB == NULL) return NULL;
ListNode *p = headA;
ListNode *q = headB;
int countA = , countB = ;
while (p->next)
{
p = p->next;
++countA;
}
while (q->next)
{
q = q->next;
++countB;
}
if (p != q) return NULL;
if (countA > countB)
{
for (int i = ; i < countA - countB; ++i)
headA = headA->next;
}
else if (countB > countA)
{
for (int i = ; i < countB - countA; ++i)
headB = headB->next;
}
while (headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA; }
};

[LeetCode160]Intersection of Two Linked Lists的更多相关文章

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

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

  3. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

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

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

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

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

  8. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  9. [Swift]LeetCode160. 相交链表 | 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. WeText项目的服务端

    WeText项目的服务端 在<WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例>文章中,我介绍了自己用Visual Studio 2015(C# 6.0 wi ...

  2. jquery.ui.accordion的修改(支持展开多个)

    原文:jquery.ui.accordion的修改(支持展开多个) 背景:原jquery.ui.accordion插件,最多只能展开一个,不能展开多个,后来在网上找到了一个基于它的一个修改版(http ...

  3. Android菜鸟的成长笔记(27)——ViewPager的使用

    ViewPager是Android 3.0以上能够使用的API. 一.ViewPager能干什么? 1.微信5.0中连带滑动用ViewPager能够轻松实现. 2.实现相似于新浪微博的导航引导界面. ...

  4. Beijing seminar: China shadow banking

    Beijing seminar: China shadow banking-张化桥-财新博客-新世纪的常识传播者-财新网 Beijing seminar: China shadow banking

  5. VC++6.0版本号程序转成VS2010版

    直接转换的时候遇到两个问题: 1.预编译头文件*.PCH找不到 2.static_cast": 无法从"void (__thiscall CView2::* )(void)&quo ...

  6. SE 2014年5月23日

    两站点 A 和 B,由于业务往来需要,所以工程师提出vpn技术,同时需要保证业务流在internet上的安全性,同时在这里站点均为固定ip地址. 通过分析以上信息,确定这里使用 IPSec VPN的主 ...

  7. WebKit爬虫

    https://github.com/emyller/webkitcrawler 一个开源的项目,可以快速入门. http://spiderformysql.com/ http://crawl.gro ...

  8. Android 监听SMS短信

    当设备接收到一条新的SMS消息时,就会广播一个包括了android.provider.Telephony.SMS_RECEIVED动作的Intent. 注意,这个动作是一个字符串值,SDK 1.0不再 ...

  9. DEDE使用AJAX无刷新提交Form表单,PHP返回结果

    $query = "INSERT INTO `{$diy->table}` (`id`, `ifcheck` $addvar)  VALUES (NULL, 0 $addvalue); ...

  10. 158个JAVA免豆精品资料汇总

    附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...