前言

 

【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. Normalize.css与Reset CSS:定义浏览器统一的默认样式

    今天在chrome上测试我的网页,发现一个<p>段落多出了一些margin,而我自己没有设定.打开f12调试,发现在一个“user agent style”栏下定义了这个margin,去g ...

  2. 使用COM口的2、3针的通断作为中端源(有一个读图像的摄像头,当把卡插到位时触发中端,防止在插卡的过程中出现不稳定的图像)

    利用串口2读,串口3发数据的特点.建立不断的发送流,再从接收端接收.如果收到,则数据畅通,否则断开.相当于产生一个中断.这样电脑对外部事件可作出反应. using System;using Syste ...

  3. 将maven打包为一个jar(可以体外加入jar)

    使用 maven-compiler-plugin插件, 在maven的pom的<build></build>标签中上加入 <build> <plugins&g ...

  4. iOS开发基础控件--UITextField

    001 //初始化textfield并设置位置及大小 002   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20 ...

  5. loadView 和 viewDidLoad、viewDidunload 的区别

    loadView 和 viewDidLoad 是 iPhone 开发中肯定要用到的两个方法. 他们都可以用来在视图载入的时候初始化一些内容. 但是他们有什么区别呢? viewDidLoad 方法只有当 ...

  6. 索引(index)

    #创建索引 create index index_name_pass on student(name,pass); create index index_name_id on student(name ...

  7. C#匿名类型 - Anonymous Types

    [C#匿名类型 - Anonymous Types] Anonymous types provide a convenient way to encapsulate a set of read-onl ...

  8. js的事件冒泡和点击其他区域隐藏弹出层

    一.前言 在编写页面的时候,我们经常使用到弹出层.对于弹出层,原本的意义就是增加与用户的交互,提升用户的好感度.如果弹出层都没有较好的体验,那何谈通过交互来提升好感... 首先提出几个弹出层的注意点: ...

  9. style多次设置行内样式

    语法 style="font-size:32px;background-color:#aaa"

  10. photoshop最全快捷键列表

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具 [M] 移动工具 [V] 套索.多边形套索.磁性套索 [L] 魔棒工具 [W] 裁剪工具 [C] 切片工 ...