【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 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.
提示:
假设两个链表有合并的情况,那么合并部分的长度一定是一样的,在合并之前长度会有所不同,所以先求出长度,然后把长的链表向前走,让他们“在同一起跑线”,然后依次比较。
代码:
/**
* 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) {
if (!headA || !headB) return NULL;
int lenA = , lenB = ;
ListNode *nodeA, *nodeB;
for (nodeA = headA; nodeA; nodeA = nodeA->next, ++lenA);
for (nodeB = headB; nodeB; nodeB = nodeB->next, ++lenB);
if (lenB > lenA) {
for (int i = ; i < lenB - lenA; ++i, headB = headB->next);
} else {
for (int i = ; i < lenA - lenB; ++i, headA = headA->next);
}
if (headA == headB) return headA;
while (headA && headB) {
headA = headA->next;
headB = headB->next;
if (headA == headB) return headA;
}
return NULL;
}
};
【LeetCode】160. Intersection of Two Linked Lists的更多相关文章
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#160. Intersection of Two Linked Lists
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)
题目地址: https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 题目内容: Write a program to fi ...
- LeetCode OJ 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 ...
- 【leetcode❤python】 160. Intersection of Two Linked Lists
#-*- coding: UTF-8 -*- #两种方法#方法1:#计算出A和B两个链表的长度分别为m.n;#长度长的链表先走m-n步,之后再一次遍历寻找#方法2:#先走到一个链表的尾部,从尾部开始走 ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- [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 ...
- [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 ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- Shell脚本编写
1.什么是Shell脚本 Shell脚本是利用 shell 的功能所写的一个程序 program,这个程序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面, 搭配正则表达式.管 ...
- 看完48秒动画,让你不敢再登录HTTP网站(附完整示例代码)
在我的 单点登录SSO示例代码 一文中,强烈不建议部署HTTP的SSO服务站点. 在此写个基于网络包嗅探的HTTP会话劫持程序,给大家一个直观的危害性展示. 示例中,我在一台Mac上登录58同城,被另 ...
- 使用faker 生成中文测试数据
https://github.com/fzaninotto/Faker/blob/master/src/Faker/Provider/zh_CN/Address.php 常用的类型都在里面. 下面是一 ...
- web乱码解决了
web容易乱码,最近有乱码了,透死了! 搞了半天,终于好了: String comment = new String(request.getParameter("comment") ...
- Fast Fourier Transform ——快速傅里叶变换
问题: 已知$A=a_{0..n-1}$, $B=b_{0..n-1}$, 求$C=c_{0..2n-2}$,使: $$c_i = \sum_{j=0}^ia_jb_{i-j}$$ 定义$C$是$A$ ...
- ACCESS数据库增强器需求及介绍
目前版本:ver1.0.0.2 现已支持cs文件浏览,高亮显示 针对如下图所示的access数据库,我想导出access数据库的所有或者部分表的表结构,还想对表进行封装,封装如下所示. using S ...
- VUE依赖webpack分别给开发环境和生产环境配置不同的常量值并在项目中动态引用
当在开发和产品上线的时候,我们经常会遇到在同一个地方由于环境的不同而地址也不同的情况,这时候如果在代码中将该地址写死,那势必会造成上线时手动改动,多人开发及多处使用该地址难以维护等一系列问题,为避免这 ...
- java加密解密研究6、MD算法家族
一.简述 MD5算法是典型的消息摘要算法,其前身有MD2.MD3和MD4算法,它由MD4.MD3和MD2算法改进而来.不论是哪一种MD算法,它们都需 要获得一个随机长度的信息并产生一个123位的信息摘 ...
- java虚拟机学习-JVM调优总结-典型配置举例(10)
以下配置主要针对分代垃圾回收算法而言. 堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理 ...
- [原创]CentOS下Radius服务器搭建
一. 实现环境: 1.系统:CentOS release 6.6 (Final) 2.需要软件包: 1) freeradius-2.1.12-6.e16.x86_64 freeradius-m ...