编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
A:           a1 → a2
                            ↘
                                c1 → c2 → c3
                            ↗            
B:  b1 → b2 → b3
在节点 c1 开始相交。
注意:
    如果两个链表没有交点,返回 null.
    在返回结果后,两个链表仍须保持原有的结构。
    可假定整个链表结构中没有循环。
    程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
详见:https://leetcode.com/problems/intersection-of-two-linked-lists/description/

Java实现:

方法一:借助栈

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null){
return null;
}
Stack<ListNode> stk1=new Stack<ListNode>();
Stack<ListNode> stk2=new Stack<ListNode>();
while(headA!=null){
stk1.push(headA);
headA=headA.next;
}
while(headB!=null){
stk2.push(headB);
headB=headB.next;
}
if(stk1.peek()!=stk2.peek()){
return null;
}
ListNode commonNode=null;
while(!stk1.isEmpty()&&!stk2.isEmpty()&&stk1.peek()==stk2.peek()){
commonNode=stk1.pop();
stk2.pop();
}
return commonNode;
}
}

方法二:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null){
return null;
}
int n=0;
ListNode head1=headA;
ListNode head2=headB;
while(head1!=null){
++n;
head1=head1.next;
}
while(head2!=null){
--n;
head2=head2.next;
}
ListNode longHead=n>0?headA:headB;
ListNode shortHead=longHead==headA?headB:headA;
n=n>0?n:-n;
for(int i=0;i<n;++i){
longHead=longHead.next;
}
while(longHead!=shortHead){
longHead=longHead.next;
shortHead=shortHead.next;
}
return longHead;
}
}

160 Intersection of Two Linked Lists 相交链表的更多相关文章

  1. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

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

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

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

  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] 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. 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)

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

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

  8. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

  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. Kubernetes实战阅读笔记--2、架构和部署

    安装Kubernetes “本书准备了4台虚拟机(CentOS 7.0系统)用于部署Kubernetes运行环境,包括一个Etcd.一个Kubernetes Master和三个Kubernetes N ...

  2. weblogic 修改控制台console访问路径 url

    出于安全的考虑需要对weblogic的console进行屏避,或者修改默认的访问路径,主要有两种方法:(这里针对weblogic8.1) 一.进入默认的控制台,例如“localhost/console ...

  3. HBase在滴滴出行的应用场景和最佳实践

    摘要: 主要介绍了HBase和Phoenix在滴滴内部的一些典型案例.文章已在CSDN极客头条和<程序员>杂志发表,应朋友邀请,分享到云栖社区,希望给大家带来启发和帮助. 背景 对接业务类 ...

  4. Android:在子线程中更新UI的三种方式

    ①使用Activity中的runOnUiThread(Runnable) ②使用Handler中的post(Runnable) 在创建Handler对象时,必须先通过Context的getMainLo ...

  5. 最短路Dijkstra算法的一些扩展问题

    最短路Dijkstra算法的一些扩展问题     很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...

  6. gitbash使用

    gitbash是什么 git bash是Windows下的命令行工具. 基于msys GNU环境,有git分布式版本控制工具. 主要用于git版本控制,上传下载项目代码. GNU环境,就是说如果你喜欢 ...

  7. android adb 源码框架分析(2 角色)【转】

    本文转载自:http://blog.csdn.net/luansxx/article/details/25203323 角色 l  服务 服务是提供特定功能的实体,接收请求,返回应答是服务直接最表现. ...

  8. HDU4513 吉哥系列故事——完美队形II Manacher算法

    题目链接:https://vjudge.net/problem/HDU-4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Me ...

  9. perl字符集处理

    本文内容适用于perl 5.8及其以上版本. perl internal form 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种u ...

  10. 出现Insufficient space for shared memory file错误解决

    今天在linux下敲命令,出现上面的错误,原来是临时文件目录(/tmp)下的空间不够了,df一看/下100%了.