找出链表的交点, 如图所示的c1, 如果没有相交返回null.

A:             a1 → a2
                               ↘
                                   c1 → c2 → c3
                              ↗           
B:     b1 → b2 → b3

我的方法是:

(1)统计两个链表的长度

(2)移动较长的链表的表头,使得让两个链表的长度一致

(3)从修正后的表头出发对比两个链表的节点是否一致,输出一致的节点,否则输出null

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int list_len(ListNode* head){
int cnt = ;
for(ListNode* now = head; now; now = now->next){
++cnt;
}
return cnt;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int al = list_len(headA);
int bl = list_len(headB);//(1)
ListNode* minh = headA;
ListNode* maxh = headB;
int cnt = bl - al;
if(al > bl) {
maxh = headA;
minh = headB;
cnt = -cnt;
}
while(cnt--){
maxh = maxh->next;
} //(2)
for( ;maxh && minh ; maxh = maxh->next, minh = minh->next){
if(maxh == minh) return maxh;
}
return NULL; //(3)
}
};

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

随机推荐

  1. css 层的嵌套

    html <div class="menu"> <ul> <li><a class="li1" title=" ...

  2. python之获取页面标签的方法

    from urllib.request import urlopen from urllib.error import HTTPError from bs4 import BeautifulSoup ...

  3. [html]兼容 IE6 IE7 的简单网页框架

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. TF-IDF 相关概念

    概念 TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度. TF-IDF加权的各种形式常被搜索引擎应用,作为文件与用户查询之间相关程度的度量或评级. 词频( ...

  5. windows server 2008 - 创建域和本机用户

    /* * ===================================================================================== * Filenam ...

  6. loadrunner11录制报 NOT PROXIED!错误,无法生成脚本

    使用loadrunner11,IE9录制完脚本,报错: [Net An. Error    (1dec:282c)] Request Connection: Remote Server @ 210.5 ...

  7. mysql数据库查询pdo的用法

    最早的php对mysql数据库查询是mysql和mysqli方法,后来php的新版本进一步封住了该方法,于是又pdo,抛开php框架,使用pdo查询数据,使用也是相当简便 <?php ini_s ...

  8. 马踏飞燕——奔跑在Docker上的Spark

    目录 为什么要在Docker上搭建Spark集群 网络拓扑 Docker安装及配置 ssh安装及配置 基础环境安装 Zookeeper安装及配置 Hadoop安装及配置 Spark安装及配置 集群部署 ...

  9. C++字符串常量

    C++字符串常量 当一个字符串常量出现于表达式中时,它的值是个指针常量.编译器把这个指定字符的一份copy存储在内存的某个位置(全局区),并存储一个指向第一个字符的指针. #include <i ...

  10. linux下使用SSL代理(SSLedge)

    refer to: https://eurekavpt.com/page/ssledge-on-linux 启动非常简单./ssledge-term-x64 -f config -D 其中的confi ...