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.

题目要求:

求两个链表的交点,如果没有,则返回NULL

要求O(n)的时间复杂度和O(1)的空间复杂度

解题思路:

1、如果不考虑空间复杂度,可以用set容器记录第一个链表的所有结点,依次遍历第二个链表,第一个存在set中的结点即为交点,否则不存在。

2、第一个链表长为x,第二个链表长为y,假设x>y,让第一个链表指针先走x-y步,(这样两个链表指针就长度对齐),然后两个链表指针一起走,如果遇到对应相等,则为交点,否则不存在。

代码:

/**
* 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 i=;
for(ListNode *p=head;p!=NULL;p=p->next)
i++;
return i;
} ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL || headB==NULL) return NULL;
int lenA=,lenB=;
ListNode *pA,*pB;
pA=headA;
pB=headB; lenA=getLength(pA);
lenB=getLength(pB); if(lenA>lenB){
for(int i=lenA-lenB;i>;i--)
pA=pA->next;
} if(lenB>lenA){
for(int i=lenB-lenA;i>;i--)
pB=pB->next;
} while(pA!=pB){
pA=pA->next;
pB=pB->next;
} if(pA==pB)
return pA;
else
return NULL;
}
};

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

  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算法题-Intersection of Two Linked Lists(Java实现)

    这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...

  4. LeetCode OJ: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. (LinkedList)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. (LeetCode 83)Remove Duplicates from Sorted Lists

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. [LeetCode 题解]:Intersection of Two Linked Lists

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Suppose an ...

  8. LeetCode题解之Intersection of Two Linked Lists

    1.题目描述 2.问题分析 使用unordered_set 将链表A中的节点地址全部插入,然后使用链表B中的每个节点在A中查找. 3.代码 ListNode *getIntersectionNode( ...

  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. BZOJ2673 [Wf2011]Chips Challenge 费用流 zkw费用流 网络流

    https://darkbzoj.cf/problem/2673 有一个芯片,芯片上有N*N(1≤N≤40)个插槽,可以在里面装零件. 有些插槽不能装零件,有些插槽必须装零件,剩下的插槽随意. 要求装 ...

  2. 【8.13校内测试】【DP】【按除数分类】【二分】

    感觉今天状态不太好啊一大早就很困,t1卡得有点久,以为三道题都是这个难度,结果难度完全是倒着排的啊!!在dp和数学上还得多练题!! 很像背包的一道DP??先不考虑树的结构,给每个点都先分配一个度数,剩 ...

  3. JavaScript 继承和数组

    前言 因为篇幅比较短,所以将JavaScript中的继承和数组进行统一写. 继承 当一个函数对象被创建的时候,Function构造器产生的函数对象会运行类似这样的代码: this.prototype ...

  4. Python学习笔记(六)—元组的操作

    元祖也是一个列表,它和list的区别是元祖里面的元素无法修改: 如果元祖里面只有一个元素的话,那么你必须在这个元素后边加上逗号,这样才是元祖的类型:否则类型会显示其他类型 元组的定义: 元祖中的方法: ...

  5. Python学习笔记(四):字符串的学习

    总结的内容: 1.字符串常用的方法 2.Python字符串格式化 3.Python字符串转义字 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...

  6. 回顾下$.ajax()方法参数

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  7. 追踪CPU跑满

    http://blog.donghao.org/2014/04/24/%e8%bf%bd%e8%b8%aacpu%e8%b7%91%e6%bb%a1/

  8. 由于拷贝的文件太大,不可能一直开着SHELL,所以让SCP后台运行

    原文地址: http://blog.itpub.net/90618/viewspace-750822/ 1:开一个终端,scp命令运行后,输入密码让其拷贝 # scp chris@221.179.1. ...

  9. arcgis连接excel出现数据库失败 外部数据库驱动程序意外错误

    微软搞事情,删除以下更新就行:win7 KB4041678 KB4041681SERVER 2008 R2 KB4041678 KB4041681WIN10 KB4041676 KB4041691SE ...

  10. Mybatis 传入List类型参数,报错:There is no getter for property named '__frch_item_0' in

    错误如下: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Re ...