Intersection of Two Linked Lists 解答
Question
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.
Solution
Key to the solution here is to traverse two lists to get their lengths. Therefore, we can move the pointer for the longer list first and then compare elements of both lists.
/**
* 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;
ListNode p1 = headA, p2 = headB;
int l1 = 0, l2 = 0, ll = 0;
while (p1 != null) {
l1++;
p1 = p1.next;
}
while (p2 != null) {
l2++;
p2 = p2.next;
}
p1 = headA;
p2 = headB;
if (l2 >= l1) {
ll = l2 - l1;
while (ll > 0) {
p2 = p2.next;
ll--;
}
} else {
ll = l1 - l2;
while (ll > 0) {
p1 = p1.next;
ll--;
}
}
while (p1 != null && p2 != null) {
if (p1.val == p2.val)
return p1;
p1 = p1.next;
p2 = p2.next;
}
return null;
}
}
Intersection of Two Linked Lists 解答的更多相关文章
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- LeetCode: Intersection of Two Linked Lists 解题报告
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(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 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
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅳ
3.1.4 无序链表中的顺序查找 符号表中使用的数据结构的一个简单选择是链表,每个结点存储一个键值对,如以下代码所示.get()的实现即为遍历链表,用equals()方法比较需被查找的键和每个节点中的 ...
- ListView之SimpleAdapter
SimpleAdapter是安卓内置的适配器,本文展示的是listview的子项为{图片,文件}组合 如下图所示: 具体代码: SimpleAdapter_test.java /* ListView ...
- jquery 鼠标图片经过效果
<script> //鼠标图片经过效果 $(function(){ $(".jione_box ").css("background-color", ...
- iOS-网络编程(一)HTTP协议
一. 网络编程基础 在移动互联网时代,几乎所有应用都需要用到网络,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力.一个好的移动网络应用不仅要有良好的UI和良好的用户体验也要具备实时更 ...
- 修改UISearchBar输入框字体颜色
UITextField *searchField = [mySearchBar valueForKey:@"_searchField"]; searchField.textColo ...
- Hadoop 写SequenceFile文件 源代码
package com.tdxx.hadoop.sequencefile; import java.io.IOException; import org.apache.hadoop.conf.Conf ...
- Working with Numbers in PL/SQL(在PL/SQL中使用数字)
This article gives you all the information you need in order to begin working with numbers in your P ...
- UIProgressView-初识IOS
好几天没更新了,学的时候太紧,没时间复习了都.今天刚好有时间,多更几个. 今天复习的是UIProgressView,我们常见使用在修改某些属性的时候经常用到,比如透明度,今天我们介绍一个简单的使用例子 ...
- 虚拟化之docker安装篇
1,docker pull centos 下载centos镜像 docker search centos 搜索镜像 2,docker images 查看本地镜像 3,do ...
- Oracle - 找不到原因的无效字符
当执行Oracle语句时,提示“无效字符”,而语句并无错误时,尝试把语句中的空格替换成半角状态的. 一般直接复制的语句会出现这种问题.