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. ES之六:ElasticSearch中Filter和Query的异同

    如下例子,查找性别是女,所在的州是PA,过滤条件是年龄是39岁,balance大于等于10000的文档: { "query": { "bool": { &quo ...

  2. zclip复制到剪切板插件有个bug

    今天发现zclip复制到剪切板插件有个bug,就是在遨游和360浏览器的兼容模式下,点击复制没响应,后来我看了页面代码,发现在这两个浏览器的兼容模式下,生成的是<object>,其他浏览器 ...

  3. 【Gearman学习笔记】分布式处理入门

    1.首先,确保你已经安装了gearmand环境并且语言绑定(language binding)已经生效. 2.涉及到的各个部分: ServerThe server, gearmand, will co ...

  4. mysql设置索引

    1.添加PRIMARY KEY(主键索引) 语法:ALTER TABLE `表名` ADD PRIMARY KEY ( `列名称` ) mysql>ALTER TABLE `table_name ...

  5. C# webbrowser实现百度知道团队邀请助手!

    [百度知道团队邀请助手] 是您快速提高百度知道团队成员数和团队排名的利器! 主要功能: 1.运用C#自带的webbrowser自动登录百度: 2.自动采集请在C#.Net分类排名下的所有用户,邀请这些 ...

  6. nms

    nms函数是保留选框中得分最高的那一个 Python代码如下 def nms(boxes, threshold, method): """ boxes: [x1, y1, ...

  7. centos7.3nginx配置二级域名过程

    nginx1.10.2 1先检查 /etc/nginx/nginx.conf 是否include  conf.d include /etc/nginx/conf.d/*.conf; 默认都是包含的,如 ...

  8. kvm安装及使用

    ****centos7安装及使用kvm: http://blog.csdn.net/github_27924183/article/details/76914322?locationNum=5& ...

  9. Ubuntu jdk 8 与 6 切换 (安装与配置)

    Switch To Oracle JDK8 Switch To Oracle JDK8 1.1 Switch Oracle JDK in the Unbuntu 14.04 Step1 : Downl ...

  10. centos配置DNS和ip

    Centos6.5 永久修改DNS地址的方法 1.配置ip地址文件 /etc/sysconfig/network-scripts/ifcfg-eth0添加一行 DNS1=8.8.8.8    #手动添 ...