题目:

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. ssl https双向验证的配置与证书库的生成

    1.SSL认证 不须要特别配置,相关证书库生成看https认证中的相关部分 2.HTTPS认证 一.基本概念 1.单向认证,就是传输的数据加密过了,可是不会校验client的来源  2.双向认证,假设 ...

  2. hdu4004(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 大致题意 二分最大跳跃能力,判断是否可以在m次内到达对岸! 分析:由于求青蛙最小弹跳能力,所以二 ...

  3. hdu1151 Air Raid,DAG图的最小路径覆盖

    点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring& ...

  4. atitit.团队建设--要不要招技术储备人才的问题

    atitit.团队建设--要不要招技术储备人才的问题 1.      人才的储备和招聘. 1 1.1.   模式1.     养兵千日,用兵一时 1 1.2. 模式2,暂时抱佛脚,也不多招一个人 1 ...

  5. Cocos2d-x教程(28)-ttf 字体库的使用

    欢迎增加 Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/37650843 通常为 ...

  6. C#文件流写入方法

    stream为服务端接收的文件流 var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的 ...

  7. 【译】ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据

    原文:[译]ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据 在本节中,你将新建一个MoviesController 类,并编写获取电影数据的代码,使用视图模板将数据展示在浏览器中. ...

  8. Java学习文件夹

    每天进步一点点,先研究一门语言深入研究下去.

  9. JFinal开发8个常见问题

    下面是8个最常见的问题总结. 1.Can not create instance of class: demo.DemoConfig. 觉得应该是你的路径有问题, 打开你项目的java build p ...

  10. 如何在 Windows Phone 8 中获取手机的当前位置

    原文 如何在 Windows Phone 8 中获取手机的当前位置 适用于:仅限于 Windows Phone 8. 本主题演示如何使用 Windows Phone 位置 API 确定手机的当前位置. ...