题目

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. 03python面向对象编程3

    案例学习 # notebook.pyimport datetime # Store the next available id for all new notes last_id = 0 class ...

  2. PHP file函数

    一.判断函数 is_file($filename) //判断是否文件 is_link($filename) //判断是否为链接符号 is_dir($filename) //判断是否为路径 is_rea ...

  3. SpringBoot之集成数据库

    一.集成 MySQL 数据库 1.1 配置 MySQL 添加依赖 <dependencies> <!--Spring 数据库相关依赖--> <dependency> ...

  4. json对象之间的转化

    json字符串转化为 1.使用JSON.parse()函数 使用eval()函数 2.json对象转化为json字符串 使用JSON.stringify()

  5. Android App学习计划

    模块化 Json Gson Fastjson Jackson EventBus GreenDao Flutter ButterKnife Dagger okhttp Rxjava/Rxandroid ...

  6. python-上传文件的几种方式

    from requests_toolbelt import MultipartEncoder import requests # from_data上传文件,注意参数名propertyMessageX ...

  7. Codecombat 游戏攻略(计算机科学三)

    第二关 赋值运算符-=字符串拼串循环语句while // 你可以把字符串连起来,或者把数字连接到字符串. // 一起唱歌,使用字符串连接: // X potions of health on the ...

  8. [luogu]P2680 运输计划[二分答案][树上差分]

    [luogu]P2680 [NOIP2015]运输计划 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n ...

  9. 【bzoj3343】教主的魔法

    *题目描述: 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始 ...

  10. 设计模式学习笔记——Composite 组合模式

    用于描述无限层级的复杂对象,类似于描述资源管理器,抽象出每一个层级的共同特点(文件夹和文件,展开事件) 以前描述一个对象,是将整个对象的全部数据都描述清楚,而组合模式通过在对象中定义自己,描述自己的下 ...