前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

2. 题意

寻找两个链表的交点。

提示:

1. 如果两个链表没有交集,那么返回NULL。

2. 链表的结果不能发生变化。

3. 两个链表都没有环。

4. 请给出O(n)时间复杂度、O(1)空间复杂度的解法。

3. 思路

分别统计链表A和链表B的长度。

如果两个链表有交集,那么从交点CP开始,后续的所有节点都应该相同。如图所示C1->C2->C3.

两个链表不同的节点分别为a1,a2; b1,b2,b3;

比较两个链表的长度la,lb;

假设la>lb,那么链表A先遍历la-lb节点。从la-lb节点开始,两个链表的长度就相同了。然后依次比较各自节点是否相同,直到找到交点或者到达链表尾部。

4: 解法

class Solution {
public:
int getListLen(ListNode *head){
int len=0;
ListNode *root=head;
while(root){
root=root->next;
len++;
}
return len;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int alen=getListLen(headA),blen=getListLen(headB);
ListNode *la=headA,*lb=headB;
if(alen>blen){
while(alen!=blen){
la=la->next;
alen--;
}
}
if(alen<blen){
while(alen!=blen){
lb=lb->next;
blen--;
}
}
while(la!=lb){
la=la->next;
lb=lb->next;
}
if(!la||!lb){
return NULL;
}else{
return la;
}
}
};

作者:Double_Win
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。  若本文对你有所帮助,您的关注推荐是我们分享知识的动力!

[LeetCode 题解]:Intersection of Two Linked Lists的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  6. 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 ...

  7. 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 ...

  8. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  9. ✡ 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 ...

  10. 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 ...

随机推荐

  1. sql developer Oracle 数据库 用户对象下表及表结构的导入导出

    Oracle数据库表数据及结构的导入导出   导出的主机与即将导入到的目标主机的tablespace 及用户名需一直!!!!!

  2. 可以兼容ie6的纯CSS三级鼠标悬停显示/隐藏菜单实现

    本来在chrome上用js写的好好的三级显隐菜单,放到ie6上一测试竟然奇葩般的会瞎闪.问题原因至今没参透,可能是我每次响应事件的处理代码过长??总之我是对ie6幻灭了,去网上搜一搜能支持ie6的下拉 ...

  3. leetcode840

    本题不清楚题意,从网上找到了python的解答,记录如下. class Solution: def numMagicSquaresInside(self, grid): ans, lrc = 0, [ ...

  4. Python2处理字符集问题

    这篇文章主要介绍了Python2.x中文乱码问题解决方法,本文解释问题原因.给出了处理办法并讲解了编码解码的一些知识,需要的朋友可以参考下 Python中乱码问题是一个很头痛的问题. 在Python3 ...

  5. PHP 连接打开新网页带参数

    PHP 连接打开新网页带参数 detail.php?ID=<?PHP echo $row['ID'];?> aa.php?ID=123 取ID参数 $aid=$_GET['ID']; 网页 ...

  6. VS2017更新后 在WIN7上找不到 stdio.h等的问题

    项目->属性->配置属性->常规->windows SDK版本.将其换成你现在的版本即可解决问题,如果不行就重新下个最新版SDK,如WIN10的.

  7. Delphi三层开发小技巧:TClientDataSet的Delta妙用

    Delphi三层开发小技巧:TClientDataSet的Delta妙用 转载 2014年10月13日 09:41:14 标签: 三层 / ClientDataSet 318 from :http:/ ...

  8. 二维码生成插件qrious及网站扫码登录的一些理解

    什么是二维码 ​ 二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型. ...

  9. ios7 适配

    1.状态栏20px高度问题 ) { [application setStatusBarStyle:UIStatusBarStyleLightContent]; self.window.clipsToB ...

  10. kubernetes基础环境配置

    一.基础环境配置 环境详情 主机名(FQDN) IP地址(NAT) 描述 linux-node1.example.com eth0:192.168.56.11 1VCPU.2G内存.一块硬盘s da5 ...