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.

Credits:
Special thanks to @stellari for adding this problem and creating all test cases.

Hide Tags

Linked List

 

     两个单项链表,判断是否存在交集,如上图很清晰,最直观的方法是
for  list1 begin to last
  for list2 begin to last
    if list2==list1 success
  end
end  
    时间是O(nm),空间挺少的O(1)。如何提高呢?
  1. 遍历list1 ,将其节点存在hash_table
  2. 遍历list2,如果已经在hash_table中,那么存在

利用hash_table 可以提升时间到O(n+m),可是空间变O(n)了

 class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<ListNode*,int> m;
while(headA!=NULL){
m[headA] = ;
headA=headA->next;
}
while(headB!=NULL){
if(m[headB]==) return headB;
headB=headB->next;
}
return NULL;
}
};
 
  那题目的最好解法,这技巧问题阿,遍历list1 后接着遍历list2,同时,遍历list2然后遍历list1,这样两个遍历的长度是一样的O(n+m),怎么判断相等呢?
 
list1:    O O O O O ⑴ ⑵ ⑶
list2:    □ □ □ □ ⑴ ⑵ ⑶
  假如list 如上,⑴ ⑵ ⑶ 为相同的节点,那么遍历list1 这样便是这样:
O O O O O ⑴ ⑵ ⑶ □ □ □ □ ⑴ ⑵ ⑶
  遍历list2 便是这样。
□  □ □ □ ⑴ ⑵ ⑶ O O O O O ⑴ ⑵ ⑶
 
合在一起看看:
O  O  O  O  O  ⑴  ⑵  ⑶  □   □  □  □   ⑴  ⑵  ⑶
□   □  □   □  ⑴  ⑵  ⑶  O  O  O  O  O  ⑴  ⑵  ⑶
 
    好了,现在规律出来了。这个逻辑出来明显,主要麻烦是在遍历一个结束后接上第二个,直接改链表不好,所以,使用flag 控制。
算法逻辑:
  1. 判断list 是否有NULL 情况
  2. 同时遍历 两个新链表
  3. 如果节点地址相同,返回
  4. 如果不相同继续遍历
  5. 遍历结束返回NULL
 #include <iostream>
#include <unordered_map>
using namespace std; /**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; /**
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<ListNode*,int> m;
while(headA!=NULL){
m[headA] = 1;
headA=headA->next;
}
while(headB!=NULL){
if(m[headB]==1) return headB;
headB=headB->next;
}
return NULL;
}
};
*/
class Solution{
public:
ListNode* getIntersectionNode(ListNode *headA,ListNode * headB)
{
ListNode * h1=headA;
ListNode * h2=headB;
if(headA==NULL||headB==NULL) return NULL;
bool flag1=true,flag2=true;
while(headA!=NULL&&headB!=NULL){
if(headA==headB) return headA;
headA=headA->next;
headB=headB->next;
if(headA==NULL&&flag1){ headA=h2; flag1 =false;}
if(headB==NULL&&flag2){ headB=h1; flag2 =false;}
}
return NULL;
}
}; int main()
{
ListNode head1();
ListNode head2();
ListNode node1();
ListNode node2();
head1.next = &node1;
node1.next = &node2;
head2.next = &node2;
Solution sol;
ListNode *ret = sol.getIntersectionNode(&head1,&head2);
if(ret==NULL) cout<<"NULL"<<endl;
else cout<<ret->val<<endl;
return ;
}
 
 
 
 
 
 

[LeetCode] Intersection of Two Linked Lists 两链表是否相交的更多相关文章

  1. 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: Intersection of Two Linked Lists 解题报告

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  3. [LeetCode] 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: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. 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——Intersection of Two Linked Lists

    Description: Write a program to find the node at which the intersection of two singly linked lists b ...

  7. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

  8. Leetcode 160 Intersection of Two Linked Lists 单向链表

    找出链表的交点, 如图所示的c1, 如果没有相交返回null. A:             a1 → a2                               ↘               ...

  9. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

随机推荐

  1. php面试必知必会常见问题

    1 说出常用的10个数组方法 我觉得数组比较最能体现PHP基础语法的一个数据结构了,下面给大家列一下常用的10个关于操作数组的函数 in_array(判断数组中是否有某个元素) implode(将数组 ...

  2. Linux 查看端口占用情况

    转自:http://www.cnblogs.com/fabulousyoung/p/4071150.html 例子,查看80端口的占用情况: lsof -i:80   或者: netstat -apn ...

  3. [LeetCode] MaximumDepth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. npm 安装 不快的解决办法

    npm config list  查看配置 npm config set prefix “c:\dev\nvm\npm”(配置用npm下载包时全局安装的包路径) npm install npm -g ...

  5. POJ3709_K-Anonymous Sequence

     题意很简单,给你若干个数字,你需要减去一些数字,使得在数列中的每个数字出现的次数不少于k次. 一开始我们都会想到是用DP,于是很快我们就可以得出状态为搞定前面i个数所需要花费的最小代价用f[i]表示 ...

  6. MySQL一主两从

    服务器说明: MySQL-Master:192.168.1. MySQL-Slave1:192.168.1. MySQL-Slave2:192.168.1. 关闭防火墙,关闭selinux 统一采用源 ...

  7. spring的事务传播特性

    PROPAGATION_REQUIRED(常用) Support a current transaction; create a new one if none exists.  支持一个当前事务;如 ...

  8. java学习5-jar包的下载以及导入

    1.出现未导入包的情况 ,表示当前jdk不 2.百度下载jar包 3.File. 未完待续http://blog.csdn.net/a153375250/article/details/5085104 ...

  9. 洛谷10月月赛R2·浴谷八连测R3题解

    早上打一半就回家了... T1傻逼题不说了...而且我的写法比题解要傻逼很多T T T2可以发现,我们强制最大值所在的块是以左上为边界的倒三角,然后旋转4次就可以遍历所有的情况.所以二分极差,把最大值 ...

  10. 【loj6436】【pkusc2018】神仙的游戏

    Portal --> pkuscD2T2(loj6436) Solution 个人觉得是道很好的法法塔题qwq 一开始的时候想偏了想到了另一种法法塔处理字符串匹配之类的奇怪技巧(万径人踪灭qwq ...