编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
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. DRF 之 路由组件

    组件路由的步骤 1.先要导入DefaultRouter from rest_framework.routers import DefaultRouter 2.实例化DeaultRouter对象 rou ...

  2. poj 2228 Naptime(DP的后效性处理)

    \(Naptime\) \(solution:\) 这道题不做多讲,它和很多区间DP的套路一致,但是这一道题它不允许断环成链,会超时.但是我们发现如果这只奶牛跨夜休息那么它在不跨夜的二十四个小时里一定 ...

  3. Immutable学习及 React 中的实践

    为什么用immutable.js呢.有了immutable.js可以大大提升react的性能. JavaScript 中的对象一般是可变的(Mutable),因为使用了引用赋值,新的对象简单的引用了原 ...

  4. 利用QBuffer和QLinkedList做数据块存储队列

    Qt中QByteArray存储数据很方便,使用QBuffer存储大块数据更方便.QBuffer类包装了QByteArray类对象,实际存储还是使用了QByteArray,但QBuffer实现了QIOD ...

  5. 深度学习入门-4.1 AND.py 源码分析

    源代码 ------------------------------------------------------------------------------------------------ ...

  6. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  7. java 读写Oracle Blob字段

    许久没有分享代码了,把这段时间写的一个Java操作Blob字段,有日子没写Java了,就当作笔记记录一下.1. [代码][Java]代码     跳至 [1] [全屏预览]package com.wa ...

  8. 后台while收发过程

    fuse_loop_mt.c 中fuse_do_work函数使用while循环在后台不断运行,每一个while循环中,主要有两个操作. 1. fuse_session_receive_buf(mt-& ...

  9. Hadoop学习资料(持续更新)

    Alex的Hadoop菜鸟教程 Hadoop资料合集 Hadoop平台和应用程序框架

  10. Bootstrap-CSS:表格

    ylbtech-Bootstrap-CSS:表格 1.返回顶部 1. Bootstrap 表格 Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: ...