lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目
两个链表的交叉
请写一个程序,找到两个单链表最开始的交叉节点。
下列两个链表:
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 两个链表的交叉的更多相关文章
- intersection of two linked lists.(两个链表交叉的地方)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 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 ...
- [LC]141题 Intersection of Two Linked Lists (相交链表)(链表)
①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整 ...
- [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 ...
- LeetCode算法题-Intersection of Two Linked Lists(Java实现)
这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...
- [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 ...
- Intersection of Two Linked Lists两链表找重合节点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
随机推荐
- android Timer and TImerTask
android Timer and TImerTask Caused by: java.lang.IllegalStateException: TimerTask is scheduled alrea ...
- 下载服务器dll文件并动态加载
1.新加一个类库 namespace ClassLibrary1 { public class Class1 { public int Add(int a, int b) { return a + b ...
- DOS环境下含包并引用第三方jar的java程序的编译及运行
DOS环境下含包并引用第三方jar的java程序的编译及运行 1.程序目录机构 bin:class文件生成目录 lib:第三方jar包目录 src:源程序文件目录 2.程序代码: 3.程序编译 jav ...
- How to running Job from a Form
For Example we wanna run a Job with name "FAN_TableList_CSV". So you must create a button ...
- 使用Animation实现Button的透明度Opacity变化
接着之前的使Button的Content变化的例子,这里给出使Button的透明度变化的写法. 前台写法: 后台写法: 效果图:Opacity的值正在变小 效果还不错,前台是用Blend生成的,后台的 ...
- c指针提高
今天看了两章C语言,于是乎编段程序复习下. 还是不清楚这些神奇的东西的到底要干嘛用... 敲完后,显得这段代码高大上 但是,想实现这个程序,需要这么写的复杂吗?==|| #include & ...
- Hadoop命令摘录
一:文件操作 1.建立目录 [hadoop@hadoop1:hadoop]$bin/hadoop dfs -mkdir testdir 在HDFS中建立一个名为testdir的目录 2.上传文件到HD ...
- WPF中的Style
一.Style基础知识 构成Style最重要的两种元素是Setter和Trigger Setter类帮助我们设置控件的静态外观风格 Trigger类帮助我们设置控件的行为风格 Setter类的Prop ...
- edge.js架起node.js和.net互操作桥梁
今天要介绍的是edge.js这个github上刚兴起的开源项目,它可以让node.js和.net之间在in-process下互操作..net版本在4.5及以上,因为.net4.5带来的Task,asy ...
- 在eclipse中使用jax-ws构建webservices服务端和客户端
服务端: package com.yinfu.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebServi ...