编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
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. 2016/06/09 ThinkPHP3.2.3使用分页

    效果图:

  2. asp.net+access实现DropDownList与RadDatePicker同步筛选

    这里没有使用SqlServer是因为老师要求使用access. 前台代码 <table style="margin:auto"> <tr><td cl ...

  3. $.post 使用案例

    $.post( aplnCommon.topUrl + 'ajaxLogin/ajaxLogin.action', { 'userLoginId' : userName, 'pwd' : userPw ...

  4. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

  5. js中的连等==和全等===

    ===是没有强制类型转换的,和其他大部分语言的==是一样的.而js中==是有类型转换的. 比如说"true"==true就是错的,Boolean("false" ...

  6. 在centos7下手工安装和配置Nginx

    一.什么是Nginx Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器,在高连接并发的情况下Nginx是Apa ...

  7. linux 一个超简单的makefile

    makefile 自动化变量:   $@ : 规则的目标文件名  例如:main:main.o test.o                    g++ -Wall -g  main.o test. ...

  8. C语言一个单链表的实现

    -- 所谓链表记住一句即可:地址不连续,大家只是握个手而已: list0.c #include<stdio.h> #include<malloc.h> typedef int ...

  9. 无线网络中的MAC协议(1)

    前文我们对传统的有线网络的MAC协议进行了分析,接下来我们在对无线网络的MAC也进行一个详细的介绍.那么无线网络中的MAC工作方式是如何的呢?无线局域网(WLAN)中MAC所对应的标准为IEEE 80 ...

  10. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...