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

Notice
  • 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.
 
Example

The following two linked lists:

A:          a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

begin to intersect at node c1.

Challenge

Your code should preferably run in O(n) time and use only O(1) memory.

解法一:

 class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// write your code here
if(headA == NULL || headB == NULL)
return NULL;
ListNode* iter1 = headA;
ListNode* iter2 = headB;
int len1 = ;
while(iter1->next != NULL)
{
iter1 = iter1->next;
len1 ++;
}
int len2 = ;
while(iter2->next != NULL)
{
iter2 = iter2->next;
len2 ++;
}
if(iter1 != iter2)
return NULL;
if(len1 > len2)
{
for(int i = ; i < len1-len2; i ++)
headA = headA->next;
}
else if(len2 > len1)
{
for(int i = ; i < len2-len1; i ++)
headB = headB->next;
}
while(headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA;
}
};

380. Intersection of Two Linked Lists【medium】的更多相关文章

  1. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  2. 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  4. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  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. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

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

  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. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

随机推荐

  1. 【Tomcat】Tomcat闪退的问题解决/Tomcat修改端口号无效

    一.  Tomcat闪退的问题解决 1.首先 确定JDK的环境变量配置正确 2.下载纯净的新的Tomcat 3.在bin\startup.bat文件中的第一行前面加入: SET JAVA_HOME = ...

  2. [Git] git shortlog 找出最懒的程序员

    转载:http://blog.csdn.net/qinjienj/article/details/7795802 场景假设:一个开发小组有10个程序员,他们用 Git 做版本控制,某一天程序员A pu ...

  3. SQL Server 更新 触发器

    - 复制代码 代码如下: create trigger TR_MasterTable_Update on MasterTable after update as if update ([Type])- ...

  4. C#动态调用webservice方法

    摘 自: http://www.hao5191.cn/a/chengxukaifa/c_/20130109/115819.html using System; using System.Collect ...

  5. Eclipse通过Spket增加JQuery提示的方法

    Eclipse通过Spket增加JQuery提示的方法 1.增加在线更新源:Help->Install New Software...->Add...->Name: "Sp ...

  6. mysql 比较隐秘的问题

    2017-11-13 13:47:27:DEBUG DubboServerHandler-192.168.30.114:20990-thread-5 com.yryz.qshop.modules.in ...

  7. Vue实例初始化的选项配置对象详解

    Vue实例初始化的选项配置对象详解 1. Vue实例的的data对象 介绍 Vue的实例的数据对象data 我们已经用了很多了,数据绑定离不开data里面的数据.也是Vue的核心属性. 它是Vue绑定 ...

  8. IFA Basics

    The inverted-F antenna is shown in Figure 1. While this antenna appears to be a wire antenna, after ...

  9. java Web笔记

    Get:标记在地址栏中 放在请求行中显示get?Username=...Password=... Post:使用的是二进制的上传下载 请求行不显示信息 数据传输量大数据隐蔽对文本没有 pst不能再浏览 ...

  10. OpenERP(odoo)开发实例之搜索检索过去3个月的数据

    转自:http://www.chinamaker.net/ OpenERP(odoo)开发实例之搜索过滤:检索过去3个月的数据 解决这个问题的重点在于 relativedelta 的应用 示例代码如下 ...