题目

两个链表的交叉

请写一个程序,找到两个单链表最开始的交叉节点。

样例

下列两个链表:

A:          a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

在节点 c1 开始交叉。

注意

  • 如果两个链表没有交叉,返回null
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
挑战

需满足 O(n) 时间复杂度,且仅用 O(1) 内存。

解题

尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决。

就如同上图,两个链表相交的部分一定在尾部的,如果两个链表尾部对齐,按照短的链表头节点开始,同时时对两个链表进行遍历,找到相同节点处就是共同的节点。

这里为了找到短链表的都节点在长链表处的位置<这里的位置是相对的,他们不一定是在一起的,这里只是为了让尾对齐>。先求两个链表的长度

假设长链表是A 长度lenA 短链表B 长度LenB

长链表头节点开始走,并lenA-=1 当lenA==lenB的时候说明链表尾部对齐了,这样就开始直接按顺序比较链表节点值是否相等了。时间复杂度是O(M+N)

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param headA: the first list
# @param headB: the second list
# @return: a ListNode
def getIntersectionNode(self, headA, headB):
# Write your code here
if headA == None:
return None
if headB == None:
return None
lenA = self.getLength(headA)
lenB = self.getLength(headB)
A = None
B = None
if lenA > lenB:
A = headA
B = headB
else:
A = headB
B = headA
tmp = lenA
lenA = lenB
lenB = tmp
while lenA>lenB:
lenA -=1
A = A.next
while A and B:
if A.val == B.val:
return A
A = A.next
B = B.next def getLength(self,head):
length = 0
p = head
while p!=None:
p = p.next;
length +=1
return length

Python Code

总耗时: 340 ms.

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// Write your code here
if(headA == null)
return null;
if(headB == null)
return null;
if(headA == headB )
return headA;
// 求 A B的长度
int lenA = getLength(headA);
int lenB = getLength(headB);
ListNode A = null;
ListNode B = null;
// A是比较长的链表
if(lenA>lenB){
A = headA;
B = headB;
}else{
A = headB;
B = headA;
int tmp = lenA;
lenA = lenB;
lenB = tmp;
}
while(lenA>lenB){
A = A.next;
lenA--;
} while(A!=null && B!=null){
if(A.val == B.val){
return A;
}
A = A.next;
B = B.next;
}
return null;
}
public int getLength(ListNode head){
int length = 0;
ListNode p = head;
while(p!=null){
length++;
p = p.next;
}
return length;
}
}

Java Code

总耗时: 2028 ms

lintcode 中等题: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(两个链表的交叉点)

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

  3. [LC]141题 Intersection of Two Linked Lists (相交链表)(链表)

    ①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整 ...

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

  5. LeetCode算法题-Intersection of Two Linked Lists(Java实现)

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

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

  7. Intersection of Two Linked Lists两链表找重合节点

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

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

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

  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. ExtJS实战 01——HelloWorld

    前言 Extjs5的发布已经有些日子了,目前的最新稳定版本是Extjs5.1.0,我们可以在官方网站进行下载.不过笔者今天访问得到的是502Bad Gateway,原因可能是sencha的nigix没 ...

  2. vs2008团队资源管理器安装步骤

    1.先装 VS2008TeamExplorer { NOTE: 要区分中英文版本Microsoft Visual Studio 2008 Service Pack 1 (iso) VS2008SP1C ...

  3. lower_bound和upper_bound

    lower_bound:返回大于或等于val的第一个元素位置 upper_bound:返回大于val的第一个元素位置 两个函数用的都是二分查找

  4. 一个JS的日期格式化算法示例

    一个JS的日期格式化算法. 例子: <script> /** * Js日期格式化算法实例 * by www.jbxue.com */ function dateFormat(date, f ...

  5. 【转】Mac 上 java 究竟在哪里,本文彻底让你搞清楚!

    这篇文章可能比较适合那些在经常在Mac下进行Java编程开发,或者经常使用Java工具的朋友.不关心Java或者不了解Java的朋友可以绕过本文哈~ 1. Mac下当你在[终端]输入java -ver ...

  6. Oracle查询出最最近一次的一条记录

    需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...

  7. CHARINDEX,PATINDEX,STUFF函数

    -- CHARINDEX函数 -- 返回字符或者字符串在另一个字符串中的起始位置. -- 语法:CHARINDEX(expression1 , expression2 [,start_location ...

  8. GOOGLE影像地图

    卫星地图高清 //  

  9. ASP.NET 运行机制续(完结)

    上一篇说到applicationInstance会执行一些列的事件.下面是我在msdn上找到有关asp.net程序生命周期相关的描述及图片 声明周期的起始 ASP.NET 应用程序的生命周期以浏览器向 ...

  10. 2815: [ZJOI2012]灾难 - BZOJ

    题目描述 Description 阿米巴是小强的好朋友.    阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的 ...