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(两个链表的交叉点)的更多相关文章

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

  2. intersection of two linked lists.(两个链表交叉的地方)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

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

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

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

  7. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

  8. LeetCode——Intersection of Two Linked Lists

    Description: Write a program to find the node at which the intersection of two singly linked lists b ...

  9. 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. Leetcode#137 Single Number II

    原题地址 遍历所有数字,统计每一位出现的次数,模3即为只出现一次的数字在那一位的情况. 代码: int singleNumber(int A[], int n) { ] = {}; ; i < ...

  2. lagstash + elasticsearch + kibana 3 + kafka 日志管理系统部署 02

    因公司数据安全和分析的需要,故调研了一下 GlusterFS + lagstash + elasticsearch + kibana 3 + redis 整合在一起的日志管理应用: 安装,配置过程,使 ...

  3. ASP.NET的一套笔试题

    1.    自定义控件如何做?答:自定义控件,跟HtmlControl或WebControl相似,编译后可以添加引用到工具栏里面,直接用鼠标拖动使用.2.界面的布局?答:表格,div3.程序的执行过程 ...

  4. HDOJ 1028 Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. IT架构之IT架构标准——思维导图

    参考: [日] 野村综合研究所系统咨询事业本部. 图解CIO工作指南. 周自恒译 人民邮电出版社,2014

  6. Telnet、FTP、SSH、SFTP、SCP

    [Telnet]著名的终端访问协议,传统的网络服务程序,如FTP.POP和Telnet,其本质上都是不安全的:因为它们在网络上用明文传送数据.用户帐号和用户口令. [telnet命令]telnet h ...

  7. javascript实现数据结构:串--堆分配存储表示

    堆分配存储表示 这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得. 结构图: 实现: function HString(){ this. ...

  8. Services学习(一)

    对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服务方式.服务将通过API触发启动或者通过IPC(Interprocess Communi ...

  9. java 继承类与接口问题

    java 先extends 继承类,再implements 继承接口 public class DataBase extends ClassBase implements Ijiekou { }// ...

  10. ZOJ 2588 Burning Bridges (tarjan求割边)

    题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...