前言

 

【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: 解法

  1. class Solution {
  2. public:
  3. int getListLen(ListNode *head){
  4. int len=0;
  5. ListNode *root=head;
  6. while(root){
  7. root=root->next;
  8. len++;
  9. }
  10. return len;
  11. }
  12. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  13. int alen=getListLen(headA),blen=getListLen(headB);
  14. ListNode *la=headA,*lb=headB;
  15. if(alen>blen){
  16. while(alen!=blen){
  17. la=la->next;
  18. alen--;
  19. }
  20. }
  21. if(alen<blen){
  22. while(alen!=blen){
  23. lb=lb->next;
  24. blen--;
  25. }
  26. }
  27. while(la!=lb){
  28. la=la->next;
  29. lb=lb->next;
  30. }
  31. if(!la||!lb){
  32. return NULL;
  33. }else{
  34. return la;
  35. }
  36. }
  37. };

作者: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. HTTP之Tcp/Ip协议的工作原理

    计算机与网络设备要相互通信,双方就必须基于相同的方法.比如,如何探测到通信目标.由哪一边先发起通信.使用哪种语言进行通信.怎样结束通信等规则都需要事先确定.不同的硬件.操作系统之间的通信,所有的这一切 ...

  2. Form表单标签的Enctype属性的作用及应用示例介绍

    Enctype :指定将数据回发到服务器时浏览器使用的编码类型.用于表单里有图片上传. 编码类型有以下三种: application/x-www-form-urlencoded: 在发送前编码所有字符 ...

  3. 【312】◀▶ arcpy 常用函数说明

    其他常用的 ArcPy 函数说明 序号 类名称   功能说明   语法 & 举例 01 RefreshActiveView   ====<<<< Description ...

  4. sqlserver,oracle,mysql等的driver驱动,url怎么写

    oracle driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521 ...

  5. ORA-00604: 递归 SQL 级别 1 出现错误 ORA-01000: 超出打开游标的最大数

    有程序没关闭游标, --打开了哪些游标 select * from v$open_cursor 在open cursor之后一定要注意要close cursor(在store procedure里更应 ...

  6. Hibernate中Session与本地线程绑定

    ------------------siwuxie095 Hibernate 中 Session 与本地线程绑定 1.Session 类似于 JDBC 的连接 Connection 2.Session ...

  7. java读取properties配置文件[转]

    网上文章常见的几种读取.properties文件的方式 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInp ...

  8. Windows 毫秒计时

    #include <windows.h> #include <iostream> using namespace std; LARGE_INTEGER MilliSecondT ...

  9. [Groovy] Groovy API

    http://www.soapui.org/about-soapui/soapui-faq.html#1-SoapUI--General-Questions 3.1.1. What is Groovy ...

  10. Web测试-day

    昨天太忙忘了写博客,今天补上: 这两天完成的工作: 我们组选定了博客园和CSDN作为对比,进行Web测试. 胡俊辉--找到了10个网页的bug,并完成了bug记录文档,并且对CSDN和博客园进行功能分 ...