LeetCode(39)-Intersection of Two Linked Lists
听歌曲初爱有感:
开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的《初爱》,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐…..
题目:
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.
思路:
- 首先这道题目的意思是判断两条链表是不是有重叠的部分,如果有返回那个交叉的节点,如果没有返回null
- 这道题目的思路是:如果有交叉,最后肯定有相同的部分,如上面那样
- 举个例子,a和b两头链条,a.length = 7,b.length = 5,用两个指针listA和listB,分别指向a和b的head,listA往后两个,开始listA和listB同步往后移动开始比较,有相同的就有交叉,返回那个节点
-
代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA;;
ListNode b = headB;;
int lengthA = 0;
int lengthB = 0;
int n = 0;
while(a != null){
a = a.next;
lengthA++;
}
while(b != null){
b = b.next;
lengthB++;
}
if(lengthA > lengthB){
n = lengthA - lengthB;
a = headA;
b = headB;
while( n > 0){
n--;
a = a.next;
}
}else{
n = lengthB -lengthA;
a = headA;
b = headB;
while(n > 0){
n--;
b = b.next;
}
}
while(a != b){
a = a.next;
b = b.next;
}
return a;
}
}
LeetCode(39)-Intersection of Two Linked Lists的更多相关文章
- [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] 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】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 ...
- Java for 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 求两个链表的起始重复位置 --------- java
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- 学习Tensorflow,反卷积
在深度学习网络结构中,各个层的类别可以分为这几种:卷积层,全连接层,relu层,pool层和反卷积层等.目前,在像素级估计和端对端学习问题中,全卷积网络展现了他的优势,里面有个很重要的层,将卷积后的f ...
- 6.QT信号和槽
新建项目SignalAndSlot项目 编写代码如下: mysignal.h #ifndef MYSIGNAL_H #define MYSIGNAL_H #include <QObje ...
- 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态
一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...
- Shell在大数据时代的魅力:从一道百度大数据面试题想到的点滴
对于在Linux下开发的同学来说,Shell可以说是一种基本功. 对于运维的同学来说,Shell可以说是一种必备的技能,而且应该要非常熟练的书写Shell.对于Release Team,软件配置管理的 ...
- 如何判断webview是不是滑到底部
getScrollY()方法返回的是当前可见区域的顶端距整个页面顶端的距离,也就是当前内容滚动的距离. getHeight()或者getBottom()方法都返回当前webview这个容器的高度 ge ...
- android:getSlotFromBufferLocked: unknown buffer: 0xf3d544c0
欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 今天运行一个小程序,退出的时候竟然打出一条错误log日志.一时慌乱,在国内网站也没找到合适的答复.通过查看国外的网站终于查到了原 ...
- JAVA之旅(二十五)——文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine
JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...
- ADO.NET常用方法释义
先列个列表,下面的就是常用的数据库操作的方法. ExecuteNonQuery 释义:对链接执行的SQL语句,并返回受影响的行数(注意:用它来执行目录操作,如查询数据库的结构,创建表等数据库对象,或通 ...
- Deep Learning with Torch
原文地址:https://github.com/soumith/cvpr2015/blob/master/Deep%20Learning%20with%20Torch.ipynb Deep Learn ...
- Oracle Enterprise Linux 64-bit 下Oracle11g的监听配置修改及测试步骤
测试环境:Oracle Enterprise Linux 64-bit (5.8版本) + Oracle 11g 64位 相关说明: Oracle11g64位软件的安装位置为/u01/app/orac ...