题目

两个链表的交叉

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

样例

下列两个链表:

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. sql server 中 bigint 和 datetime 性能比较

    -- 创建表 create table Test_tbl ( ID varchar(40) primary key nonclustered, IntCol int, DateCol datetime ...

  2. VisualVM 监控

    一:服务器端: 找到 jstatd 所在目录 find / -name jstatd 在此目录下添加 jstatd.all.policy 文件 cat /usr/java/jdk1.7.0_51/bi ...

  3. CentOS设置服务开机启动的方法

    CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后 ...

  4. PHP取二进制文件头快速判断文件类型的实现代码

    通过读取文件头信息来识别文件的真实类型. 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别,PHP扩展中提供了类似 exif_im ...

  5. python 字典内置方法get应用

    python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...

  6. C# 枚举,传入int值返回string值

    需求:1:子公司负责人2:人事3:审批人4:签批人 5:管理员  传入值为1,2,3,4,5这个数字的某一个.需要返回他们的中文描述. 一下忘记该怎么写了...后来百度下查出来了..记录下当个小工具吧 ...

  7. Query execution was interrupted, max_statement_time exceeded

    版本:5.6.16 群里看见一个问题,在备份的时候,报如下错误:[root@B28-19-75 bak]# mysqldump -root -p --single-transaction --mast ...

  8. FastLoad错误 — RDBMS error 2634

    我们来看一下下面这条语句: BEGIN LOADING stu_flERRORFILES error_1, error_2;   如果此时已经存在error_1或error_2表,那么将会报错,信息如 ...

  9. 调皮的转义之addslashes

    背景: php自5.3版本开始废除set_magic_quotes_runtime函数,并在5.4及以后版本中移除了该函数 今天程序在向mysql插入一个serialize序列化后的数组时,由于一个数 ...

  10. Linux - 升级+编译kernel

    For upgrading present kernel to linux-next kernel, we need to follow below steps. 1. Check present k ...