一天一道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. npm run dev 出错的解决办法

    bogon:~ yan$ cd my-project bogon:my-project yan$ npm run dev > my-project@1.0.0 dev /Users/yan/my ...

  2. PHP开发中Redis安装(CentOS6.5)

    1.安装Redis 1 wget http://download.redis.io/releases/redis-3.2.8.tar.gz 2 tar xzf redis-3.2.8.tar.gz 3 ...

  3. jquery easyui datagrid设置行样式 不可删除某行

    rowStyler: function (index,row) { if (parseInt(row.ksrs) > 0) { return 'color:red'; } }, onLoadSu ...

  4. Linux安装JProfiler监控tomcat

    下载JProfiler包wget http://download-keycdn.ej-technologies.com/jprofiler/jprofiler_linux_9_2.rpm 安装JPro ...

  5. geotrellis使用(四十)优雅的处理请求超过最大层级数据

    前言 要说清楚这个题目对我来说可能都不是一件简单的事情,我简单尝试. 研究 GIS 的人应该都清楚在 GIS 中最常用的技术是瓦片技术,无论是传统的栅格瓦片还是比较新颖的矢量瓦片,一旦将数据切好瓦片就 ...

  6. ng-book札记——内置指令

    Angular提供了一些内置指令(directive),这些添加在HTML元素(element)上的特性(attribute)可以赋予更多的动态行为. NgIf ngIf指令用于在某个条件下显示或者隐 ...

  7. 粗糙的es6 -> es5转换正则集

    (r'() => {}','function () {return {}}'), # (r'\{\.\.\.(.+?)\}','Object.assign({}, \\1)') , # (r'( ...

  8. 搜索引擎solr和elasticsearch

    刚开始接触搜索引擎,网上收集了一些资料,在这里整理了一下分享给大家. 一.关于搜索引擎 搜索引擎(Search Engine)是指根据一定的策略.运用特定的计算机程序从互联网上搜集信息,在对信息进行组 ...

  9. rbac数据库设计

    1 rbac数据库设计 RBAC基于资源的访问控制(Resource-Based Access Control)是以资源为中心进行访问控制分享牛原创,分享牛系列,分享牛.rbac 用户角色权限资源表如 ...

  10. Swift基础之两指拉动图片变大变小

    我们在使用APP的时候,有时会发现有些图片可以通过两指进行放大.缩小,今天就实现这样的一种效果,比较简单,不喜勿喷.... var imageVi:UIImageView! = nil    var ...