题目

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
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.

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
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.

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
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.
Explanation: The two lists do not intersect, so return null.

代码

class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = getLen(headA), lenB = getLen(headB);
if(lenA>lenB){
for(int i=0; i<lenA-lenB; i++){
headA = headA->next;
}
}
else{
for(int i=0; i<lenB-lenA; i++){
headB = headB->next;
}
}
while(headA&&headB&&headA!=headB){
headA = headA->next;
headB = headB->next;
}
return (headA==headB) ? headA : NULL;
}
private:
int getLen(ListNode *list){
int cnt = 0;
ListNode *tmp = list;
while(tmp){
tmp = tmp->next;
++cnt;
}
return cnt;
}
};

思路

先计算出两个链表的长度,然后进行比较,将较长的链表缩短(即将头节点指针向后移),使得两个链表长度一致,然后让指针同时同步长迭代,当发现地址相同时则知道当前节点开始共用。

LeetCode——160 Intersection of Two Linked Lists的更多相关文章

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

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

  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

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

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

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

  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

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

随机推荐

  1. npm 在安装的时候提示 没有权限操作的解决办法 Error: EACCES: permission denied

    十分感谢https://blog.csdn.net/ldqsxsl/article/details/75059607的帮助! 错误原因:权限错误,需要root用户. 解决办法:就是把用户目录下的 .n ...

  2. express 设置跨域

    app.use(function (req, res, next) {     res.header('Access-Control-Allow-Origin', 'http://localhost: ...

  3. DevOps书单:调研了101名专家,推荐这39本必读书籍

    任何一个领域都遵循从新人到熟手,从熟手到专家的路径.在成长过程中,DevOps人经常会陷入没人带,没人管,找不到职业方向的迷茫. DevOps是在商业演进与企业协作的进化过程中诞生的一个全新职业,被很 ...

  4. spring动态调用方法

    有的时候为了程序的灵活性,需要根据参数动态的调用方法.代码框架大致spring为主,下面是具体代码: 接口: 实现类(实现类中有一个从spring容器中取的对象) 这是最初我直接用反射去调用的代码: ...

  5. SSM常用配置文件头模板

    web.xml文件头 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...

  6. MSSQL时间格式转换

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: ), 时间一, ) 结果: :/*时间一般为getdate()函数或数据表里的 ...

  7. Spring Cloud(2)主要组件应用实例

    SpringCloud SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.负载均衡.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行 ...

  8. Vuex-全局状态管理【传递参数】

    src根目录 新建store文件夹,新建index.js 作为入口 在store文件夹中 新建modules文件夹 modules文件夹中,新建 a.js b.js 2个文件 a.js const s ...

  9. 代理修饰词weak/assign/strong的区别

    基于项目报错: WebViewJavascriptBridgeBase 中定义:@property (assign) id <WebViewJavascriptBridgeBaseDelegat ...

  10. 【bzoj2733】[HNOI2012]永无乡

    题目描述: 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到 ...