letecode [160] - Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
题目大意:
给定两个链表,找到它们的第一个公共节点并返回。
理 解:
从第一个公共节点开始,两链表后面的节点都是公共的。
计算两个链表的长度,从长度相同的节点开始往后遍历,并比较p==q,若相等,则当前节点为第一个公共节点;不等则继续往后找。
代 码 C++:
/*** 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==NULL || headB==NULL) return NULL;int lenA=0,lenB=0;ListNode *p = headA;ListNode *q = headB;while(p!=NULL){lenA++;p = p->next;}while(q!=NULL){lenB++;q = q->next;}p = headA;q = headB;while(lenA>lenB){p = p->next;lenA--;}while(lenA<lenB){q = q->next;lenB--;}while(p!=NULL){if(p==q){return p;}p = p->next;q = q->next;}return NULL;} };
运行结果:
执行用时 :72 ms, 在所有C++提交中击败了84.85%的用户
转载于:https://www.cnblogs.com/lpomeloz/p/11002364.html
letecode [160] - Intersection of Two Linked Lists相关推荐
- LeetCode(160): Intersection of Two Linked Lists
Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...
- [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从零单排】No.160 Intersection of Two Linked Lists
题目 Write a program to find the node at which the intersection of two singly linked lists begins. 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 ...
- 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 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 160. Intersection of Two Linked Lists
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- leetcode python3 简单题160. Intersection of Two Linked Lists
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...
最新文章
- 对于随机森林的通俗理解
- 数据中心服务器机架是什么?
- 关于ESP8266 GPIO中断使用的总结
- 【机器学习】机器学习概述
- 2021-04-10 【数据库导数】数字类型的列如果位数过长,变为科学计数法问题
- springboot中得注解_Spring以及SpringBoot中的常用的注解小结
- 中央音乐学院计算机研究生,2020北京中央音乐学院硕士研究生招生复试电子音乐作曲、电子音乐技术理论等考生须知...
- ubuntu14.04下修改python默认版本
- Mysql统计同一字段不同值的个数
- 什么是自然语言处理,自然语言处理的技术优势和应用领域
- 全球首场神经影像人机对决:AI战胜25位医界“最强大脑”!
- 小程序---宿主环境(常用组件、api)---02
- JAVA-IDEA开发10个小手段
- TCP连接大量CLOSE_WAIT状态问题排查
- IDA PRO 静态反汇编与OllyDbg动态调试实战技巧汇总
- 如何用 Python 识别车牌
- 申宝投资-三大股指开盘逐渐下跌
- mac时间机器删除旧备份
- redis 用scan 代替keys,hgetAll
- DIY一块DS3231的高精度时钟模块,让时钟DS3231/DS1302自动识别
热门文章
- EF 执行存储过程方法
- cgi、wsgi、uwsgi 与 uWSGI
- MySQL 5.7.18 zip 文件安装过程
- 《软件需求规格说明书》 ---学士之路
- Java初级学习(转载http://www.cnblogs.com/biehongli/p/5737612.html)
- uva 12426 Counting Triangles 计算几何
- 终于开通我自己的cnblogs了,自我恭喜下··
- 编译bluez-5.25 通过 Linux环境下运行
- java socket字符串_Java Socket Bug:从Socket的InputStream读取字符串
- cp linux 显示进度条_Unix/Linux/Mac os下 文件互传