题目描述

编写一个程序,找到两个单链表相交的起始节点。

例如,下面的两个链表:

A:          a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

在节点 c1 开始相交。

注意:

  • 如果两个链表没有交点,返回 null.
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。

解题思路

首先分别获得两个链表的长度,然后让较长的链表先走两个长度的差值,使得两链表尾部对齐。然后两个链表再同时向后移动,并比较节点是否相等。

代码

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getLength(ListNode *head){
int len = ;
ListNode *node = head;
while(node){
len++;
node = node->next;
}
return len;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = getLength(headA), lenB = getLength(headB);
if(lenA < lenB) return getIntersectionNode(headB, headA);
int p = lenA - lenB;
ListNode *nodeA = headA, *nodeB = headB;
while(p){
nodeA = nodeA->next;
p--;
}
while(nodeA){
if(nodeA == nodeB) break;
nodeA = nodeA->next;
nodeB = nodeB->next;
}
return nodeA;
}
};

LeetCode 160. 相交链表(Intersection of Two Linked Lists)的更多相关文章

  1. LeetCode 160: 相交链表 Intersection of Two Linked Lists

    爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...

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

  3. Java实现 LeetCode 160 相交链表

    160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...

  4. LeetCode 160——相交链表(JAVA)

    编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...

  5. LeetCode 160 相交链表

    题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], li ...

  6. leetcode 160相交链表

    暴力解法当然可以遍历两个链表,不过time O(mn) space O(1)暂且不说, 方法一:双指针, time O(m+n),space O(1) 可以对比判断环形链表的快慢指针法. 这种方法构思 ...

  7. LeetCode 160. 相交链表 (找出两个链表的公共结点)

    题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点. 如下面的两 ...

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

  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. IEnumerable<T>和IQuryable<T>的区别

    https://stackoverflow.com/questions/1578778/using-iqueryable-with-linq/1578809#1578809 The main diff ...

  2. vue关于路由容易忽略的点

    1.去掉导航里的# 在router.js中 export default new Router{ mode:'history' } 2.指定激活项的class 在router.js中 export d ...

  3. AD转换 XPT2046

    应用电路 引脚功能描述 控制位命令 控制字节各位描述 单端模式输入配置 差分模式输入配置 掉电和内部参考电压选择 应用电路 AIN0:检测转换电位器模拟信号,控制字命令寄存器值为0x94或者0xB4 ...

  4. 第二章、前端之css

    目录 第二章.前端之css 一.css介绍 二.css语法 三.css几种引入方式 四.css选择器 五.css属性相关 六.css盒子模型 第二章.前端之css 一.css介绍 css(Cascad ...

  5. 翻译应用将在Win8.1系统中取消下载安装

    自Windows8.Windows Phone 7.1和Windows Phone 8受到影响之后,微软又正式宣布停止对翻译应用提供支持服务.Microsoft Translator这款应用将从Win ...

  6. Error:Execution failed for task ':app:compileDebugJavaWithJavac'

    百度一下呗 查找了各种解决方案,都不对症. 最后发现,造成这种异常的原因有很多.具体的还是要去终端编译,查看到底是什么地方出错了,然后具体问题具体分析. 终端进入项目的根目录,然后输入命令 ./gra ...

  7. C和指针--链表

    1.链表的基本概念 链表(linked list)是一些包含数据的节点的集合.链表中的每个节点通过链或指针连接在一起.程序通过指针访问链表中的节点.通常节点是动态分配的. 2.链表的分类 链表可分为: ...

  8. Hadoop_28_MapReduce_自定义 inputFormat

    1. 自定义inputFormat 1.1.需求: 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件,此时就需要有相应解决方案; 1.2.分析: 小文件的优化 ...

  9. Linux磁盘及文件系统管理2

    创建文件系统: 格式化:低级格式化(分区之前进行,划分磁道).高级格式化(分区之后对分区进行,创建文件系统) 元数据区,数据区 元数据区: 文件元数据:inode(index node) 大小.权限. ...

  10. 编译原理实战——使用Lex/Flex进行编写一个有一定词汇量的词法分析器

    编译原理实战--使用Lex/Flex进行编写一个有一定词汇量的词法分析器 by steve yu 2019.9.30 参考文档:1.https://blog.csdn.net/mist14/artic ...