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 ...
随机推荐
- {}+[] = ? 和 []+{} = ? 浅谈JS数据类型转换
参加公司技术嘉年华第一季(前端.服务端)的间隙,陈导问了我一个问题:{}+[] 和 []+{}两个表达式的值分别是什么?根据我的理解我觉得结果应该都是"[object Object]&quo ...
- Smart ——jiaoyou模板
<!--{foreach $vip_data as $key=>$volist}--> <!--{if $key==0 ||$key==1||$key==5||$key= ...
- web前端 - 模态对话框
代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- ThinkPHP视图查询详解
ThinkPHP视图查询详解 参考http://www.jb51.net/article/51674.htm 这篇文章主要介绍了ThinkPHP视图查询,需要的朋友可以参考下 ThinkP ...
- 9. KNN和Sparse构图
一.前言 图是一种重要的数据结构,本文主要表示图像的无向图.所谓无向图是指,图的节点间通过没有方向的边连接. 无向图的表示: 无向图G=<V,E>,其中: 1.V是非空集合,称为顶点集. ...
- mycat源码分析
http://www.cnblogs.com/fernandolee24/p/5196367.html
- HTML5-常见的事件- beforeunload事件
当我们在日常访问某些网站时,关闭当前网页时出现如下提示: beforeunload 事件就可以完成这样的事情,该事件发生时机:页面卸载之前,可以通过它来取消卸载并继续使用原有页面. 为了显示上面弹出对 ...
- javascript操作json总结
原文:http://www.cnblogs.com/worfdream/articles/1956449.html JSON(JavaScript Object Notation) 是一种轻量级的数据 ...
- 使用do...while的方法输入一个月中所有的周日
do{ var date = Number(prompt('请输入一个月的总天数')); var start = (prompt('请输入一个月的一号是周几')); for(var i=0;i< ...
- 被sjy带刷题#1
笔记[问题描述]给定一个长度为m的序列a,下标编号为1~m.序列的每个元素都是1~n的整数.定义序列的代价为 你现在可以选择两个数x和y,并将序列a中所有的x改成y.x可以与y相等.请求出序列 ...