【leetcode】Intersection of Two Linked Lists(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.
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.
思路:简单题 遍历长度 走差值 再同步走 答案里给了个转圈圈的思路 感觉没有自己的好 太乱了
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL)
{
return NULL;
}
int lenA = ;
int lenB = ;
ListNode * pa = headA;
ListNode * pb = headB; while(pa->next != NULL)
{
pa = pa->next;
lenA++;
}
while(pb->next != NULL)
{
pb = pb->next;
lenB++;
} if(pa != pb) return NULL; pa = headA;
pb = headB;
while(lenA > lenB)
{
pa = pa->next; lenA--;
}
while(lenB > lenA)
{
pb = pb->next; lenB--;
}
while(pa != pb)
{
pa = pa->next;
pb = pb->next;
} }
【leetcode】Intersection of Two Linked Lists(easy)的更多相关文章
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 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 ...
- [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 ...
- [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 ...
- 【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 ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 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 ...
- [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 ...
随机推荐
- linux/centos下安装nginx(rpm安装和源码安装)详细步骤
Centos下安装nginx rpm包 ...
- PHP学习笔记 - 进阶篇(1)
PHP学习笔记 - 进阶篇(1) 数组 数组定义 $arr = array();表示创建一个空数组,并把创建的空数组赋值给变量$arr. 索引数组初始化 PHP有两种数组:索引数组.关联数组. 索引和 ...
- IOS UI 笔记整理回顾
注意手势会冒泡上抛,一个view没有实现的手势,如果父类view有实现,父视图就处理,如果不想让父视图处理,就把本视图添加到底层window上 setMasksToBounds:YES imageVi ...
- OC5_类别
// // NSString+Reverse.h // OC5_类别 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 ...
- 一个JS内存泄露实例分析
在看JS GC 相关的文章时,好几次看到了下面这个设计出来的例子,比较巧妙,环环相扣. var theThing = null; var replaceThing = function () { ...
- C++ 的隱式型別轉換
先上一段代碼, 這段代碼竟然可以編譯過,我的老天! class Boo { Boo(int c){ cout << "I'm Boo"; } }; void do_so ...
- C++ 文件读写方案选型
严格来说, 有 3 种风格. UNIX 底层读写库 c 语言 stdio 标准库 iostream 流 一般的工程中, 底层读写库封装程度太低, 需要自己处理缓存和很多通用的异常场景. 不适合. 网络 ...
- oracle 之路目录
oracle linux单机安装 oracle windows单机安装创建实例卡死解决办法 oracle rac安装 HPDL380G8平台11.2.0.3 RAC实施手册 pl-sql develo ...
- 数据库连接池c3p0和dbcp
现在常用的开源数据连接池主要有c3p0.dbcp和proxool三种,其中: hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样 ...
- 禁止指定目录执行php文件
我们设置网站权限的时候,有些目录不得不设置让http服务器有写入权限,这样安全隐患就来了.比如discuz x2的 data目录,这个必须要有写入限,论坛才能正常运行,但有的黑客可能就会利用这个目录上 ...