题目:

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. 最佳新秀SSH(十三)——Spring集装箱IOC分析和简单的实现

    时间最近一段时期,"集装箱"这个词一直萦绕在我的耳边,连吃饭.睡在我的脑海里蹦来蹦去的. 由于这几天的交流时间.讨论,对于理解容器逐渐加深. 理论上的东西终归要落实到实践,今天就借 ...

  2. FOJ 1591 —— Coral的烦恼

    #include<stdio.h> int main() { __int64 n,i,sum,l,r; while(scanf("%I64d",&n)!=EOF ...

  3. (step 8.2.13)hdu 1524(A Chess Game)

    题目大意 : 在一个 有向无环图顶点上面有几个棋子, 2个人轮流操作, 每次操作就是找一个棋子往它能够移 动的地方移动一格, 不能操作的人输. 输入第一行 为一个 N , 表示有 N 个顶点 0 -& ...

  4. poj2245Lotto(最基础的dfs)

    题目链接: 啊哈哈,点我点我 思路:最開始画好搜索状态,然后找好结束条件,最好预推断当前找到的个数和能够找到的是否大于6就可以.. 题目: Lotto Time Limit: 1000MS   Mem ...

  5. ssl https双向验证的配置与证书库的生成

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

  6. debian下使用siege进行压力测试

    一:siege siege是开源的一个测试工具,可以对指定文本的URL列表进行负载测试,也可以在执行其他请求前让某个请求休眠,从而让你感觉某个用户在转移到web应用的下一个文档前正在读取该文档. ht ...

  7. c# Unicode字符串的解码

    前两天工作中遇到个奇怪的问题,一个unicode字符串(即“\uXXXX”形式)变量,调用HttpUtility.UrlDecode解码过后,还是原样,要么就是乱码状态.无奈之下只能自己写一个解码函数 ...

  8. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...

  9. SE 2014年4月16日

    一. 描述BGP路由协议中  BGP路由携带 AS-PATH/ next-hop  / ORIGIN /  local-preference 属性的特点! BGP协议中的AS-PATH是AS列表,用来 ...

  10. 实战:sqlserver 2008 扩展事件-XML转换为标准的table格式

    --假设已经存在Event Session删除 IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQu ...