一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:两个单向链表,有一段重复区域,找出其中的第一个交叉节点。

解题思路:题目要求时间复杂度O(n)和空间复杂度O(1),所以利用辅助空间的方法都不行。

如果两个链表会交叉,那么他们的最后一个节点肯定相同,如果是双向链表,可以从尾节点开始,找到第一个出现分离的节点即可。可是,题目要求不能破坏初始链表。这种方法也行不通。

如果两个链表的长度一样的话,从头开始往后,可以找到第一个交叉点。

记链表的长度为len1和len2,可以让长链表先走abs(len1-len2)步,再两个一起往后找。即可找到第一个交叉点。

具体解释见代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA = getlength(headA);//统计链表A的长度
        int lenB = getlength(headB);//统计链表B的长度
        int diflen = lenA-lenB;//计算差值
        //lenA<lenB的情况
        ListNode *longList = headA;//lenA<lenB的情况
        ListNode *shortList = headB;
        //lenA>lenB的情况
        if(diflen<0)
        {
            longList = headB;
            shortList = headA;
            diflen = -diflen;
        }
        while(diflen>0&&longList!=NULL)//让长链表先走diflen步
        {
            longList = longList->next;
            diflen--;
        }
        while(longList!=NULL&&shortList!=NULL)//两个链表一起往后走
        {
            if(longList->val == shortList->val) return longList;//找到交叉节点
            else{
                longList = longList->next;
                shortList = shortList->next;
            }
        }
        return NULL;
    }
    int getlength(ListNode *head)
    {
        int n = 0;
        ListNode *phead = head;
        while(phead!=NULL)
        {
            phead = phead->next;
            n++;
        }
        return n;
    }
};

【一天一道LeetCode】#160. 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 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 ...

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

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

  8. Java [Leetcode 160]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

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  10. Leetcode 160 Intersection of Two Linked Lists 单向链表

    找出链表的交点, 如图所示的c1, 如果没有相交返回null. A:             a1 → a2                               ↘               ...

随机推荐

  1. UDA机器学习基础—评估指标

    这里举例说明 混淆矩阵  精确率 召回率  F1

  2. java集合之HashMap源码解读

    源自:jdk1.8.0_121 HashMap继承自AbstractMap,实现了Map.Cloneable.Serializable. HashMap内部是由数组.链表.红黑树实现的 变量 // 默 ...

  3. class-map与policy-map 配置与qos模版

    将subnet-a 与subnet-b 归入类class1 中.Router1(config)# ip access-list extended subnet-a Router1(config-std ...

  4. ServiceStack 简单服务搭建

    1:定义数据实体 因为ServiceStack是基于请求参数来定义请求路由的,所以关键的是请求参数一定要定义好,同时可以在请求参数上自定义路由名和请求方式,作为对外接口名 上代码: namespace ...

  5. 如何上传本地项目到gitHub解决方案

    最近有人有人问到我怎么将新创建的本地代码上传到github上,这里简单的记录一下,我喜欢使用命令行,这里全用命令行来实现,不了解Git命令的可以去了解下. 1.  建立本地仓库,cd到你想要上传文件的 ...

  6. 创建OpenGL Context(WGL)

    创建OpenGL Context(WGL) 创建OpenGL Context是初始化OpenGL的一部分.只有在此之后才能使用OpenGL. 关于platform的注意事项 创建OpenGL cont ...

  7. Web缓存(一) - HTTP协议缓存

    为什么要使用 Web 缓存 Web缓存一般分为浏览器缓存.代理服务器缓存以及网关缓存,本文主要讲的是 浏览器缓存,其它两种缓存大家自行去了解下. Web 缓存游走于服务器和客户端之间.这个服务器可能是 ...

  8. 两个activity之间透明过渡效果和经验

    来看下效果图: 大致效果解释: 1. 当用户点击登录时logo下滑一定距离 2. 下滑后旋转90时 变化图标 3. 继续旋转90度 4. 然后移动到左上角 透明度渐变到上个activity 最后销毁当 ...

  9. tomcat内存溢出解决,java.lang.OutOfMemoryError: PermGen space

    今天遇到了一个java.lang.OutOfMemoryError: PermGen space异常问题,一直解决不了,根据网上修改了tomcat的配置文件,但是还是解决不了,最后是通过如下方式解决的 ...

  10. 【SSH系列】静态代理&&动态代理

    从设计模式说起 代理模式是二十三中设计模式中的一种,代理模式就是指由一个代理主题来操作真实的主题,真实的主题执行具体的业务操作,而代理主题负责其她相关业务,简而言之,代理模式可以由以下三个部分组成: ...