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 example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
解题思路:
先遍历两个list的长度,然后长的减去短的,之后同时遍历即可,JAVA实现如下:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null)
return null;
int lengthA=0,lengthB=0;
ListNode tempA=headA,tempB=headB;
while(tempA!=null){
tempA=tempA.next;
lengthA++;
}
while(tempB!=null){
tempB=tempB.next;
lengthB++;
}
if(lengthB>lengthA){
tempA=headA;
headA=headB;
headB=tempA;
int temp=lengthA;
lengthA=lengthB;
lengthB=temp;
}
int length=lengthA-lengthB;
while(length-->0)
headA=headA.next;
while(headA!=null){
if(headA==headB)
return headA;
headA=headA.next;
headB=headB.next;
}
return headA;
}
最新文章
- querySelector系列方法相比 getElementsBy 系列方法有什么区别?
- android帧动画,移动位置,缩放,改变透明度等动画讲解
- signalr遇到的问题汇总
- java 图片处理工具类
- Mvc网站发布到IIS
- 前端编码规范(4)—— CSS 和 Sass (SCSS) 规范
- [backbone]backbone.js
- hdu 4409 LCA
- Spring之Spring MVC
- 今天遇到的i++问题之记录
- Jtree(节点的渲染+资源管理器)(2)
- Delphi 的动态数组
- Python - 定制pattern的string模板(template) 具体解释
- BFS和DFS详解
- Python第一天接触心得
- 二:Maven中pom.xml元素详解
- MySQL复制相关变量
- java基础(五)-----关键字static
- 《Blue_Flke》 团队项目用户验收评审
- 【Python】解决测试依赖之 Mock模块的基本使用
热门文章
- easyVS
- Vijos1901 学姐的钱包
- CVE-2014-0160 Heartbleed Vul Analysis &;&; OpenSSL Cryptographic Software Library Bug
- iOS代码工具箱再续
- HD1847 Good Luck in CET-4 Everybody!(巴什博弈)
- 在visual studio2012中如何使用localDB具体讲解
- spring事务学习(转账案例)(一)
- Jquery 表单操作
- 用开源Look&;Feel (Substance)写 漂亮的Swing应用程序
- oracle中Blob和Clob类型的区别