leetcode: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==NULL || headB==NULL) return NULL;
- ListNode *result=NULL;
- while(headB){
- if(headA && headA->val == headB->val) {
- result = headA;
- }
- while (headA)
- {
- if (headA->next && headA->next->val == headB->val) {
- result=headA->next;
- }
- headA = headA->next;
- }
- if(headB->next) headB= headB->next;
- }
- return result;
- }
- };
会出现超时:Time Limit Exceeded
看来还是得认真审题加分析,找到解题的关键特征来提高效率。(一个星期没刷题就变得头脑简单了)
最简单的代码是这样子滴:
- class Solution {
- public:
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- while (headA && headB) {
- if (headA->val < headB->val)
- headA = headA->next;
- else if (headA->val > headB->val)
- headB = headB->next;
- else if (headA->val == headB->val)
- return headA;
- }
- return nullptr;
- }
- };
当然也可以是这样的思路:
我们可以遍历两个链表得到各自的长度,然后将长度更长的链表向前移动,使两个链表进行对齐,之后一起遍历,直到找到第一个相同的节点。
- class Solution {
- public:
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- auto currA = headA, currB = headB;
- int countA = 0, countB = 0;
- while (currA) {
- currA = currA->next, countA++;
- }
- while (currB) {
- currB = currB->next, countB++;
- }
- int diff = std::abs(countA - countB);
- if (countB > countA) { swap(headA, headB); }
- while (diff--) {
- headA = headA->next;
- }
- while (headA != headB) {
- headA = headA->next, headB = headB->next;
- }
- return headA;
- }
- };
或者:
为了节省计算,在计算链表长度的时候,顺便比较一下两个链表的尾节点是否一样,若不一样,则不可能相交,直接可以返回NULL
- class Solution {
- public:
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- if(headA == NULL || headB == NULL)
- return NULL;
- ListNode* iter1 = headA;
- ListNode* iter2 = headB;
- int len1 = 1;
- while(iter1->next != NULL)
- {
- iter1 = iter1->next;
- len1 ++;
- }
- int len2 = 1;
- while(iter2->next != NULL)
- {
- iter2 = iter2->next;
- len2 ++;
- }
- if(iter1 != iter2)
- return NULL;
- if(len1 > len2)
- {
- for(int i = 0; i < len1-len2; i ++)
- headA = headA->next;
- }
- else if(len2 > len1)
- {
- for(int i = 0; i < len2-len1; i ++)
- headB = headB->next;
- }
- while(headA != headB)
- {
- headA = headA->next;
- headB = headB->next;
- }
- return headA;
- }
- };
leetcode:Intersection of Two Linked Lists(两个链表的交叉点)的更多相关文章
- [LeetCode] Intersection of Two Linked Lists 两链表是否相交
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- intersection of two linked lists.(两个链表交叉的地方)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [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 ...
- [LeetCode] 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 Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- LeetCode——Intersection of Two Linked Lists
Description: Write a program to find the node at which the intersection of two singly linked lists b ...
- Intersection of Two Linked Lists两链表找重合节点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- MAC下搭建web开发环境
具体做法,参照此链接:http://mallinson.ca/osx-web-development/ Mac系统本身自带apache和PHP,MySQL可以安装也可以不安装 web开发的IDE可以是 ...
- MVC bundle(CSS或JS)
无论是有asp还是asp.net,还是php做网站经验的都知道当我们需要css或者js文件的时候我们需要在<head></head>标签中间导入我们需要的js或者css文件的路 ...
- BZOJ 1087状态压缩DP
状态压缩DP真心不会写,参考了别人的写法. 先预处理出合理状态, 我们用二进制表示可以放棋子的状态,DP[I][J][K]:表示现在处理到第I行,J:表示第I行的状态,K表示现在为止一共放的棋子数量. ...
- Appstore提交 被拒绝
Reasons 16.1: Apps that present excessively objectionable or crude content will be rejected 16.1 We ...
- zend studio 快捷键收集
Ctrl + / 单行注释 Ctrl + Shift + / 添加块注释 Ctrl + Shift + \ 移除块注释 Ctrl + Shift + F 调整代码格式 Ctr ...
- Delphi托盘类 收集
收集的两个托盘程序: 1. 托盘区就是在windows的状态栏下方显示时钟.输入法状态的地方, 要把你的程序显示在托盘区: 下面是一个托盘类,只要把下面粘贴到文本文件中,改成TrayIcon.pas, ...
- POJ 2533 Longest Ordered Subsequence
题目描述:LIS(Longest Increasing Subsequence)模板题 分析:O(n^2)的方法 状态表示:d[i]表示以i结尾的最长上升子序列长度 转移方程:d[i]=max{ 1, ...
- 虚拟专用网络VPN
寒假回到家里需要下载论文,怎样才能访问学校图书馆的数据库呢?解决方法是学校图书馆在内网中架设一台VPN服务器,VPN服务器有两块网卡,一块连接内网,一块连接公网.然后就可以通过互联网找到VPN服务器, ...
- (转)android屏幕适配
声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息 原文作者: zhuangyujia 原文地址: http://my.eoe.cn/zhuangyujia/archiv ...
- js的正则表达式
正则表达式(regular expression)是一中描述字符模式的对象,js的RegExp类表示正则表达式,String与RegExp都定义了相应的方法来操作正则表达式,比如模式匹配,文本检索和替 ...