160 Intersection of Two Linked Lists 相交链表
编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
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 相交链表的更多相关文章
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
- 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 ...
- [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] 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 ...
- 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 ...
- [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 ...
- ✡ 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 ...
- (链表 双指针) 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 ...
随机推荐
- python day- 6 is 和 ==的区别 encode 和 decode
1.is 和 == 的区别. == 是由来判断左右两边的内容是否相等. is 是用来判断内存地址是否相同. 引进 id ( )函数 小数据池: 对于字符串 ,数字 ,bool 值进行 id()计 ...
- java序员必备的十大技能
想成为一名出色的Java程序员么?本文将为大家重点介绍程序员必备的十大技能,成就您的梦想. 1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样 ...
- Struts2的运行流程及其工作原理
1 服务开启,配置文件初始化 2 用户访问login请求 3 进入web.xml文件中我们配置的核心控制器(filter) 4 核心过滤器中有一个FilterDispatcher,FilterDisp ...
- 使用NetBeans生成jar包,并在jar包中添加资源
在NetBeans中,执行Clean and Build便可得到jar文件 若要在jar中添加资源,先用压缩软件打开jar,然后将资源拖进当前归档文件即可 使用Class.getResource(St ...
- 用Qt编写的计算文件MD5值的Demo
Dialog.ui <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0& ...
- var和let的区别(详解)
1. 作用域 通过var定义的变量,作用域是整个封闭函数,是全域的 . 通过let定义的变量,作用域是在块级或是子块中. function varTest() { var x = 1; if (tru ...
- extjs 4 checkboxgroup Panel的简单用法
Ext.require([ 'Ext.tree.*', 'Ext.data.*', 'Ext.window.MessageBox', 'Ext.tip.*' ]); Ext.onReady(funct ...
- UIProgress控件的属性和方法
进度条控件是IOS开发中一个简单的系统控件,使用总结如下: 初始化一个进度条: - (instancetype)initWithProgressViewStyle:(UIProgressViewSty ...
- 解决IE浏览器部分版本不支持background-size属性问题
background-size是CSS3新增的属性,现在有很多浏览器都支持CSS3了.但是IE浏览器有些版本还是不支持,比如IE8,IE9也有些CSS3的属性会支持,但是有些也不支持.在这里就了解一下 ...
- Android Studio运行出现项目 错误:非法字符:“\ufeff”
导入项目时,遇到编码问题. 主要原因是: eclipse可以自动把UTF-8+BOM文件转为普通的UTF-8文件,但Android Studio需要重新转换一下编码 解决方法: 将编码格式UTF-8+ ...