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. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  2. 重温PHP之选择排序

    思路:一组数中,选出最小者与第一个位置数交换,然后在剩余数中再找最小者与第二个位置数交换,依次类推,循环到倒数第二个数和最后一个数比较为止. 测试代码: 结果:

  3. XStream转换Java对象与XML

    1.引入需要的jar包,在pom.xml中配置依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId> ...

  4. Netdata----Linux 性能实时监测工具

    https://my-netdata.io/ https://github.com/firehol/netdata/wiki http://soluck.iteye.com/blog/2291618

  5. xarmain使用Forms编译android工程出现support_r19.0.1.zip支持包错误

    第一次使用xarain下载Forms程序,提示一下错误. C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.t ...

  6. 系统字体的Regular、Light等几种名称的区别

    以苹果系统中的PingFang SC系列字体为例,其中常见的有下面几种类型可以细分如下. PingFang SC ExtraLight         苹方 特细 PingFang SC Light  ...

  7. JAVA常见算法题(三十五)

    判断一个整数能被几个9整除. public static void main(String[] args) { f(729); f(730); } public static void f(int n ...

  8. [MAC OS] NSButton tag 获取

    @IBAction func switchContentLayout(_ sender: Any) { let button : NSButton = sender as! NSButton;}

  9. [转]C++之运算符重载(2)

    上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题.++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又 ...

  10. 学习笔记:状态压缩DP

    我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...