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

For example, the following two linked lists:

begin to intersect at node c1.

Example 1:

  1. Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
  2. Output: Reference of the node with value = 8
  3. Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:

  1. Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
  2. Output: Reference of the node with value = 2
  3. Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:

  1. Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
  2. Output: null
  3. Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
  4. Explanation: The two lists do not intersect, so return null.

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.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

中文大意就是返回两个链表的交点

可以用双指针,不过不是慢指针和快指针,而是用两个结点分别指向两个链表中。然后开始迭代,如果最后两个结点相等,说明不是相遇就是都指向了NULL了,另外,如果其中一个结点到了NULL,就指向另一个链表。(emmmm,用语可能有错,不过意思应该一样吧,笑哭)

C++代码:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  12. if(headA == NULL || headB == NULL)
  13. return NULL;
  14. ListNode *a = headA;
  15. ListNode *b = headB;
  16. while(a != b){
  17. a = a?a->next:headB;
  18. b = b?b->next:headA;
  19. }
  20. return a;
  21. }
  22. };

(链表 双指针) 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 解题思路

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

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

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

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

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

  9. Leetcode 160 Intersection of Two Linked Lists 单向链表

    找出链表的交点, 如图所示的c1, 如果没有相交返回null. A:             a1 → a2                               ↘               ...

随机推荐

  1. How to install Windows 7 SP1 on Skylake

    Download gigabyte windows usb installation tool http://www.gigabyte.cn/WebPage/-79/usb.html get Wind ...

  2. How to proof RSA

    欧拉函数 :欧拉函数是数论中很重要的一个函数,欧拉函数是指:对于一个正整数 n ,小于 n 且和 n 互质的正整数(包括 1)的个数,记作 φ(n) . 完全余数集合:定义小于 n 且和 n 互质的数 ...

  3. codeforces285C

    Building Permutation CodeForces - 285C Permutation p is an ordered set of integers p1,  p2,  ...,  p ...

  4. ElasticSearch原理

    Elasticsearch-基础介绍及索引原理分析 最近在参与一个基于Elasticsearch作为底层数据框架提供大数据量(亿级)的实时统计查询的方案设计工作,花了些时间学习Elasticsearc ...

  5. Listview 包含 edittext 的解决方案

    转载:https://blog.csdn.net/qq_28268507/article/details/53666576 一. 前几天在群里聊天,碰到一个哥们问listview的itemview中包 ...

  6. [踩过的坑]Elasticsearch.Net 官网示例的坑

    经过昨天的ElasticSearch 安装,服务以及可以启动了,接下来就可以开发了,找到了官网提供的API以及示例,Es 官方提供的.net 客户端有两个版本一个低级版本: [Elasticsearc ...

  7. Android学习第7天

    这次大都是广播案例,在笔记中不予展示注: a.做开机启动时,在广播类中用到Intent需要这三行代码 Intent intent1 = new Intent(context, MainActivity ...

  8. CODEFORCES掉RATING记 #1

    时间:2017.7.16晚 比赛:Educational Codeforces Round 25 比赛开始前去睡觉了...开始后5min才起来 一进去就点开AB,B先加载好,就先做了B.读完题后发现是 ...

  9. zabbix 监控 WEB 应用性能

    1.介绍使用 zabbix_sender 发送采集的 WEB 状态值,使用 pycurl 来采集 WEB 状态zabbix_sender发送数据,需保证主机名与zabbix server记录的主机名一 ...

  10. [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】

    题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...