【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 ...
随机推荐
- iOS开发——毛玻璃透明
主要实现的代码如下: self.rateInfoView是定义好的控制属性控件 可以改变透明度的值来改变毛玻璃透明的效果 // 虚拟交易费率弹窗 - (void)showRateInfo{ self. ...
- Repeater内RadioButton.GroupName失效
最近在做项目时遇到要在repeater中显示多个radiobutton并且实现单选功能,于是很自然地就加上了GroupName,但事实是不行的,在repeater中的radiobutton呈现到页面的 ...
- 移动web屏幕适配方案
刚进部门就被拉去趟移动端Web的浑水,视觉稿是按照640px设计的.那如何做屏幕适配呢?当然想到的第一方法就是问前辈了,问他们之前怎么做的,前辈说直接按视觉稿来,我说640太大了,他说除以2啊,按32 ...
- MyBatis参数传入集合之foreach动态sql
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...
- 利用TCP 客户端---->服务端 传送文件到指定路径,并返回一个友好的回馈
首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下. 错误的代码如下: package com.TCP.java; import java.io.File; impor ...
- IE=edge,chrome=1的META信息详解
这几天在玩 HTML5 ★ Boilerplate,注意到meta信息中有这么一句: 复制代码 代码如下: <meta http-equiv="X-UA-Compatible" ...
- jQuery(function($){...})与(function($){...})(jQuery)知识点分享
写jQuery插件时一些经验分享一下. 一.推荐写法 jQuery(function($){ //coding }); 全写为 jQuery(document).ready(function($){ ...
- 为云饰数据库添加Index
Asset Collection: 1. _id_ 2. CategoryId_1_Date_-1 3. CategoryId_1_Id_1 4. CategoryId_1_Name_1 5. Cat ...
- ECSHOP如何解决购物车中商品自动消失问题
最近有客户反映关于ECShop购物车的问题:需要加入多个商品到购物车时,发现之前加入到购物车的商品都自动消失了,只有最后一次加入购物车的商品在里面.那么,这是什么原因呢? 因为ECShop的SESSI ...
- 一个基于python的即时通信程序
5月17日更新: 广播信息.用户列表.信息确认列表以及通信信息,从原来的用字符串存储改为使用字典来存储,使代码更清晰,更容易扩展,具体更改的格式如下: 广播信息(上线): { 'status': 信息 ...