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 ...
随机推荐
- C在控制台上实现鼠标画图功能
#include <windows.h> #include <stdio.h> #include <string.h> HANDLE hOut; HANDLE hI ...
- 没想到你是这样的UDP
UDP是国际标准化组织为互联网设定的标准中的传输层中的一个协议.TCP/IP协议簇是一个很庞大的家族,但是今天我们就来看一看这个面向无连接的传输层在Java中是怎样通过编程实现的. 原理性知识 在Ja ...
- Java EE 之 过滤器入门学习与总结(1)
使用Filter技术来配合开发会使得开发变得简单起来.简单的一个例子就表现在"乱码问题"上.不使用Filter的话,我们有可能需要为每一个网页设置字符编码集,如request.se ...
- iOS中 基于LBXScan库二维码扫描 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后 ...
- Android样式(style)和主题(theme)资源介绍-android学习之旅(五十六)
样式(style)资源 代码示例 <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- JAVA之旅(二十五)——文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine
JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...
- (国内)完美下载android源代码(文章已经丢失)
刚刚文章莫名其妙的丢了,我重写了一篇,http://blog.csdn.net/song19891121/article/details/50099857 我们在很多时候需要下载android源代码进 ...
- Integration between SharePoint 2013 and CRM 2013 (On-Premise)
具体步骤可见下面的链接 https://community.dynamics.com/crm/b/msdynamicscrmtips/archive/2014/01/27/integration- ...
- 有关Spring注解@xxx的零碎知识
在Java的Spring开发中经常使用一些注解,例如 @XXX 等等,在网上看到收集整理碎片知识,便于懒人计划^=^... 过去,Spring使用的Java Bean对象必须在配置文件[一般为a ...
- Linux下触摸屏驱动程序分析
[摘要: 本文以linux3.5--Exynos4412仄台,剖析触摸屏驱动焦点内容.Linux下触摸屏驱动(以ft5x06_ts为例)须要懂得以下学问: 1. I2C协定 2. Exynos4412 ...