Intersection of Two Linked Lists(LIST-2 POINTER)
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)的更多相关文章
- 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 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- ✡ 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 ...
随机推荐
- Jquery判断IE6等浏览器的代码
这好像是由几篇代码接在一起的,原文均来自网络,不记得出处了~ jquery中利用navigator.userAgent.indexOf来判断浏览器类型,并进行了一下处理,如果不想使用jquery,稍为 ...
- Solr Web增加Basic安全性验证
Solr在5.0版本后,不再提供war包部署的方式,取而代之的是内置了jetty服务, 但是我们发现其中并没有内置任何安全性相关检查,任何人如果知道了我们的外网地址就能直接访问并修改其中的索引 ...
- 第3章 文件I/O(3)_内核数据结构、原子操作
3. 文件I/O的内核数据结构 (1) 内核数据结构表 数据结构 主要成员 文件描述符表 ①文件描述符标志 ②文件表项指针 文件表项 ①文件状态标志(读.写.追加.同步和非阻塞等状态标志) ②当前文件 ...
- GameObject数组
一.声明 GameObject[] rock = ]; 二.遍历 可以先声明一个GameObject的数组GameObject[], 然后把需要遍历的对象放进去,然后可以用下面两种方式遍历:1.for ...
- Android ffmpeg rtmp(source code)
souce code: Android.mk 编译生成APK需要调用的so文件 LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODU ...
- spark sql 的性能调优
Caching Data in Memory 其他调优参数
- 搭建Hive 2.1.1 基于Hadoop 2.6.1 和 Ubuntu 16.0.4 记录
Hadoop Hive Hbase 对应版本 Hive官网下载 我们以Hadoop版本作为参考适配Hive Hbase即可, Hadoop版本是2.6.1 所以可以选择Hive1.2.1以上版 ...
- OpenACC 计算圆周率(简单版)
▶ 书上的计算圆周率的简单程序,主要是使用了自定义函数 #include <stdio.h> #include <stdlib.h> #include <math.h&g ...
- OpenACC 计算规约时发现的小坑
▶ 使用 OpenACC 的 parallel 构件来计算规约,主要想说的是 win10 pgi 和 win10 WSL pgi 结果的不同和关于 for 循环的一个小坑 ● 正常的代码 #inclu ...
- sql server 定期自动清理日志
https://blog.csdn.net/dqs78833488/article/details/51372491