LeetCode OJ 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
- If the two linked lists have no intersection at all, return
null. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
这个题目是要找出两个链表的交叉点,该如何解决呢?如果两个链表相交,我们从A链表出发移动一段距离alen,出B列表出发移动一段距离blen,那么会发现他们指向同一个节点c1。那么这个距离是多少呢?
我们把每一个链表看成两段,不相交的一段和相交的一段。相交的一段对于两个链表长度是一样的,不想交的一段链表的长度是不同的。如果我们分别计算出两个链表的长度,然后计算他们长度的差值f,然后在较长的链表上先移动距离f,再同时从A,B链表出发开始遍历,若发现他们指向同一个节点,则他们相交并返回相交的点,若他们不想交,则会遍历到链表的尾部,则返回null。代码如下:
/**
* 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) {
ListNode p1 = headA, p2 = headB;
int len1 = 0, len2 = 0;
while (p1 != null) { //求链表A的长度
p1 = p1.next;
len1++;
}
while (p2 != null) { //求链表B的长度
p2 = p2.next;
len2++;
}
p1 = headA;
p2 = headB;
if (len1 > len2) { //计算链表长度的差值并在较长的链表上向后移动|len1-len2|
for (int i = 0;i < len1 - len2; i++) {
p1 = p1.next;
}
} else {
for (int i = 0;i < len2 - len1; i++) {
p2 = p2.next;
}
}
while (p1 != p2) { //向后遍历链表A和链表B,找到相交的节点,若遍历到最后,返回null
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}
LeetCode OJ 160. Intersection of Two Linked Lists的更多相关文章
- 【一天一道LeetCode】#160. Intersection of Two Linked Lists
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 【LeetCode】160. Intersection of Two Linked Lists
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- LeetCode OJ: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【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 解题思路
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(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 ...
- [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 ...
随机推荐
- try{}catch{}finally{}的手记
try{ System.out.println("执行try"); int = 6 / 0; return 1; }catch(Exception e){ System.out.p ...
- 使用minidom来处理XML的示例(Python 学习)(转载)
作者网站:http://www.donews.net/limodou/archive/2004/07/15/43609.aspx 一.XML的读取.在 NewEdit 中有代码片段的功能,代码片段分 ...
- Uploadifive 使用教程【结合七牛】
Uploadify是一个基于JQuery的多文件上传JS组件,高度定制,两个版本可供选择.flash版本在最新的Safari等不再支持flash的浏览器或者一些手机浏览器中就无法正常的加载使用,因此推 ...
- 【转】Python BeautifulSoup 中文乱码解决方法
这篇文章主要介绍了Python BeautifulSoup中文乱码问题的2种解决方法,需要的朋友可以参考下 解决方法一: 使用python的BeautifulSoup来抓取网页然后输出网页标题,但是输 ...
- R绘图学习笔记(二)-
依照计量对比药物A和药物B的响应情况 #计量向量 dose <- c(20,30,40,45,60) #药物A的反应向量数据 drugA <- c(16,20,27,40,60) #药物B ...
- HDU 2212 DFS
Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...
- linshi_temp_erweima_html
<!doctype html><html><head><meta charset="utf-8"><meta content= ...
- js正则函数中test和match的区别
test是RegExp的方法,参数是字符串,返回值是boolean类型. match是String的方法,参数是正则表达式,返回值是数组. <script type="text/jav ...
- jquery之全选全不选
<input type="checkbox" onclick="selall(this)" />全选/全不选 <input type=&quo ...
- digitalocean教程:你应该知道的10件事
DigitalOcean VPS性价比高,全球拥有多处机房,无须备案,非常适合守规矩的中国用户使用.digitalocean优惠码.digitalocean速度.digitalocean评测是网友关心 ...