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之八:elasticsearch2.x下的JAVA API示例

    D:\soft\elasticsearch\elasticsearch-2.1.0\lib package com.dxz.es; import java.net.InetAddress; impor ...

  2. shell 6基本运算符

    shell支持多种运算符: * 算数运算符 * 关系运算符 * 布尔运算符 * 字符串运算符 * 文件测试运算符 算数运算符 + 加 `expr $a + $b` 结果为 30 - 减 `expr $ ...

  3. testNG断言

    https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat 断言:Hamcrest - Matchers 对象: ...

  4. [UE4]产生开枪特效

  5. ElasticSearch 索引模块——全文检索

    curl -XPOST http://master:9200/djt/user/3/_update -d '{"doc":{"name":"我们是中国 ...

  6. Spring AOP课程实战

  7. Python序列化和反序列化vsJSON

    # -*- coding: utf-8 -* """没有嵌套类的类 author: Jill usage: """ import json ...

  8. redis实现分布式锁 转自importnew 记录一下

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...

  9. MYSQL体系结构-来自期刊

    MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...

  10. 练习Laravel Homestead的安装

    1 安装VirtualBox和Vagrant 在启动Homestead环境之前,你必须安装VirtualBox(https://www.virtualbox.org/wiki/Downloads)和V ...