1. Intersection of Two Linked Lists My Submissions QuestionEditorial Solution

    Total Accepted: 72580 Total Submissions: 239762 Difficulty: Easy

    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.

思路:两条链表如果相交,那从交点开始,后面一定都是相等的

那如何求第一个交点呢?

时间复杂度:O(n)

要么返回空

有交点时,说明尾部是对齐的,要找到第一个交点,只要让长的链表先走len1-len2步,这里假设len1是那条长链,那么此时再同时走,相遇的第一个节点便是交点

/**
* 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;//有一为空返回
if(headA==headB)return headA;//头部相同返回
ListNode *l1=headA,*l2=headB;
int len1=1,len2=1;
while(l1->next){//遍历记录长度
l1=l1->next;
len1++;
}
while(l2->next){
l2=l2->next;
len2++;
}
ListNode *p = len1>len2?headA:headB; //p为长链表
ListNode *psmall=len1>len2?headB:headA; //psmall为短链表
int count=abs(len2-len1);
while(count--){
p=p->next;
}
while(p!=NULL&&psmall!=p){
p=p->next;
psmall = psmall->next;
}
if(p!=NULL)return p; //有交点
else return NULL; //无交点 }
};

51-Intersection of Two Linked Lists的更多相关文章

  1. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

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

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

  3. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

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

  7. LeetCode_160. Intersection of Two Linked Lists

    160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...

  8. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

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

  10. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

随机推荐

  1. 【二食堂】Beta - Scrum Meeting 5

    Scrum Meeting 5 例会时间:5.18 18:30~18:50 进度情况 组员 当前进度 今日任务 李健 1. 划词功能已经实现,继续开发,完善文本区域交互,调用API issue 1. ...

  2. TDengine在数益工联工业物联采集平台建设中的初步实践

    作者:易永耀 夏杭泰 邓炜兴 公司介绍 数益工联致力于打造基于数据流+价值流的离散制造业数字化软件:应用新一代的物联网技术与丰富的现场交互手段,融合工业工程精益思想,为离散制造业客户的数字化升级提供从 ...

  3. Qt学习-模仿Qt实现一个colorbutton

    1.mycolorbutton.h #include<QToolButton> #include<QtGlobal> #include<QColor> class ...

  4. QEvent

    QEvent类是所有事件类的基类,每一个对象都包含事件参数.Qt的主事件循环(QCoreApplication::exec())从事件队列中接收本地窗口系统的事件,并将它们翻译成QEvent,将这些事 ...

  5. Allure快速入门

    1.关于Allure     Allure框架是一个灵活轻量级多语言测试报告工具,它不仅可以以WEB的方式展示简介的测试结果,而且允许参与开发过程的每个人从日常执行的测试中最大限度的提取有用信息.   ...

  6. reorder-list leetcode C++

    Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→- You ...

  7. hdu 3032 Nim or not Nim? (SG,然后找规律)

    题意: n堆石头,每堆石头个数:s[1]...s[n]. 每人每次可以选择在一堆中取若干个(不能不取),或者把一堆石头分成两堆(两堆要都有石头). 无法操作者负. 数据范围: (1 ≤ N ≤ 10^ ...

  8. redux 的简单实用案例

    redux 的简单实用案例 整体思想与结构 创建一个Action 创建一个Reducer 创建Store 在App组件开始使用 整体思想与结构 文件目录如下: 构建 action,通过创建一个函数,然 ...

  9. Idea tomcat debug按钮灰色无法运行

    打开Project Structure 2.选中src,点击按钮关闭界面,重启idea即可

  10. Spring 之 BeanFactory 源码 - 接口分析

    一.BeanFactory的基本类体系结构(接口为主):