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 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.
题目标签:Linked List
题目给了我们两个链表,让我们找到它们是否有交点,返回那个交点。
试想一下,如果是两个长度相等的链表,那么我们只需要遍历链表,比较两个点 是否 相等 就可以了。
所以,我们首先要得到两个链表的长度,如果不一样长,那么把长的链表先走,走到 和 另外一个链表一样长度的时候,开始比较两个点 是否 相等来找到交点;如果没有,那么最后就返回null。
如果两个链表有交点的话,那么从交点前一个点开始,两个链表从这里开始,之后的长度一定是相等的。
Java Solution:
Runtime beats 41.31%
完成日期:06/09/2017
关键词:singly-linked list
关键点:让更长的链表先走完多余的部分
/**
* 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 cursor1 = headA;
ListNode cursor2 = headB;
int len1 = getListLength(cursor1);
int len2 = getListLength(cursor2); if(len1 > len2)
{
for(int i=0; i<len1-len2;i++)
cursor1 = cursor1.next;
}
else if(len1 < len2)
{
for(int i=0; i<len2-len1;i++)
cursor2 = cursor2.next;
} while(cursor1 != null)
{
if(cursor1 == cursor2)
return cursor1; cursor1 = cursor1.next;
cursor2 = cursor2.next;
} return null;
} private int getListLength(ListNode head)
{
ListNode cursor = head;
int len = 0; while(cursor != null)
{
cursor = cursor.next;
len++;
} return len;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 160. Intersection of Two Linked Lists (两个链表的交点)的更多相关文章
- [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点
		方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ... 
- [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: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 ... 
- [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 ... 
- ✡   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 ... 
- Java for 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 ... 
随机推荐
- 华硕(ASUS)X554LP笔记本在64位win7下无线网络连接问题
			还是那台华硕(ASUS)X554LP笔记本,无线网卡为 Qualcomm Atheros AR956x,某天换了个环境,WIFI(此处简称为WIFI网A)就连不上网了.手机.其它笔记本用无线连接都没问 ... 
- 我的MYSQL学习心得链接
			http://www.cnblogs.com/lyhabc/p/3793524.html 
- 大理石在哪儿(Where is the Marble?,Uva 10474)
			现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1 ... 
- UVA - 10048 Audiophobia(Floyd求路径上最大值的最小)
			题目&分析: 思路: Floyd变形(见上述紫书分析),根据题目要求对应的改变判断条件来解题. 代码: #include <bits/stdc++.h> #define inf 0 ... 
- Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)
			题目: The park management finally decided to install some popular boxing machines at various strategic ... 
- Spring核心技术(五)——Spring中Bean的作用域
			前文概述了Spring的容器,Bean,以及依赖的一些信息,本文将描述一下Bean的作用域 Bean的作用域 当开发者定义Bean的时候,同时也会定义了该如何创建Bean实例.这些具体创建的过程是很重 ... 
- nyoj 93 汉诺塔(三)(stack)
			汉诺塔(三) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度 ... 
- hdu  2844 多重背包二进制优化
			//http://www.cnblogs.com/devil-91/archive/2012/05/16/2502710.html #include<stdio.h> #define N ... 
- Oracle删除约束和主键的语句
			https://blog.csdn.net/xue_yanan/article/details/78210654?locationNum=8&fps=1 
- kendo grid Hierarchy
			Hierarchy grid中不能使用下面的这段代码,会造成传值传不过来,把下面的代码注释,不用models,直接用form表单传值就行,暂时没搞明白为什么 //parameterMap: funct ... 
