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.

FIRST TRY

/**
* 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 = ;
int lenB = ;
ListNode* pA = headA;
ListNode* pB = headB;
while(pA->next)
{
lenA++;
pA = pA->next;
}
while(pB->next)
{
lenB++;
pB = pB->next;
}
if(pA == pB)
{
pA = headA;
pB = headB;
if(lenA > lenB)
{
lenA -= lenB;
while(lenA-- > ) pA = pA->next;
}
else if(lenA < lenB)
{
lenB -= lenA;
while(lenB-- > ) pB = pB->next;
}
while(pA!=pB)
{
pA = pA->next;
pB = pB->next;
}
return pA;
}
else return NULL;
}
};

Result: Accepted

Intersection of Two Linked Lists(LIST-2 POINTER)的更多相关文章

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

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

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

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

  4. 2016.5.24——Intersection of Two Linked Lists

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

  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 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

随机推荐

  1. ubuntu solute two different terminals cmd

    sudo update-alternatives --config x-terminal-emulator select 1. gnome-xterminal

  2. 新手搭建 nginx + php (LNMP)

    配置源 纯净的Centos 6.5系统 配置163yum源 (这个比较简单,百度能解决很多问题) 开始 安装  开发软件包:yum  -y groupinstall  "Developmen ...

  3. 基于WMI获取本机真实网卡物理地址和IP地址

    using System; using System.Collections.Generic; using System.Management; using System.Runtime.Intero ...

  4. Python之函数——内置函数

    内置函数(Built-in Functions) 截止到3.6版本,python一共为我们提供了68个内置函数.它们就是python提供给的可以直接拿来使用的所有函数,接下来让我们一起认识一下这些函数 ...

  5. uva-10474-枚举-水题

    题意:看代码 #include<stdio.h> #include<iostream> #include<sstream> #include<queue> ...

  6. TCP keep-alive翻译

    原文链接: http://www.freesoft.org/CIE/RFC/1122/114.htmhttp://www.freesoft.org/CIE/RFC/1122/index.htm 实现T ...

  7. windows10系统telnet登陆

    参考网站:https://jingyan.baidu.com/article/acf728fd498e9ff8e510a322.html windows10系统以及没有telnet服务器端了. 需要单 ...

  8. leetcode949

    public class Solution { public string LargestTimeFromDigits(int[] A) { ); ; ; foreach (var nums in l ...

  9. delphi IDE RAD 丢失的快捷键 Ctrl+Shift+F

    丢失的Ctrl+Shift+F delphi IDE RAD 丢失的快捷键 Ctrl+Shift+F Find inFiles 为什么呢?RAD Berlin安装了cnpack1.1.0.829后文件 ...

  10. UI5-文档-4.11-Pages and Panels

    在完成了应用程序结构的所有工作之后,是时候改进我们的应用程序的外观了.在这一步中,您还将了解控件聚合. Preview A panel is now displaying the controls f ...